How can I print the value of the argument that raised the exception in Java?

I write a parser for csv files, and sometimes I get a NumberFormatException. Is there an easy way to print the value of an argument that throws an exception?

At the moment, I have many try-catch blocks that look like this:

String ean; String price; try { builder.ean(Long.parseLong(ean)); } catch (NumberFormatException e) { System.out.println("EAN: " + ean); e.printStackTrace(); } try { builder.price(new BigDecimal(price)); } catch (NumberFormatException e) { System.out.println("Price: " + price); e.printStackTrace(); } 

I would like to write something like:

 try { builder.ean(Long.parseLong(ean)); } catch (NumberFormatException e) { e.printMethod(); // Long.parseLong() e.printArgument(); // should print the string ean "99013241.23" e.printStackTrace(); } 

Is there any way to improve my code? And do this type of printing / logging more programmatically?

UPDATE: I tried to implement what Joachim Sauer answered, but I don't know if I got everything right, or if I could improve it. Please give me some feedback. Here is my code:

 public class TrackException extends NumberFormatException { private final String arg; private final String method; public TrackException (String arg, String method) { this.arg = arg; this.method = method; } public void printArg() { System.err.println("Argument: " + arg); } public void printMethod() { System.err.println("Method: " + method); } } 

Wrapper Class:

 import java.math.BigDecimal; public class TrackEx { public static Long parseLong(String arg) throws TrackException { try { return Long.parseLong(arg); } catch (NumberFormatException e) { throw new TrackException(arg, "Long.parseLong"); } } public static BigDecimal createBigDecimal(String arg) throws TrackException { try { return new BigDecimal(arg); } catch (NumberFormatException e) { throw new TrackException(arg, "BigDecimal.<init>"); } } } 

Usage example:

 try { builder.ean(TrackEx.createBigDecimal(ean)); builder.price(TrackEx.createBigDecimal(price)); } catch (TrackException e) { e.printArg(); e.printMethod(); } 

EDIT: Same question, but for .NET: In .net exception, how to get stacktrace with argument values

+4
source share
3 answers

You can easily implement such detailed information about custom exceptions, but most existing exceptions do not provide much more than a detailed message and a throwing exception.

For example, you can transfer all your parsing requests to a utility class that catches a NumberFormatException and throws a custom exception instead (possibly a NumberFormatException extension).

An example where some additional information is carried through an SQLException exception that has a getErrorCode() and a getSQLState() .

+9
source

Create a method, such as private parse (String value, int type) , that does the actual parsing work, including exception handling and logging.

  parse(ean, TYPE_LONG); parse(price, TYPE_BIG_DECIMAL); 

Where TYPE_ is just something to tell the method how it should parse the value.

+3
source

As with the other sentence, you can extract Long.parseLong(ean) in your own method (either privately in the class, or publicly available for another class of the class).

This new method will handle any user logic, and you can test it in isolation. Hurrah!

+1
source

Source: https://habr.com/ru/post/1304159/


All Articles