Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding info to an exception

Tags:

java

exception

I'd like to add information to a stack trace/exception.

Basically I've got something like this as of now, which I really like:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.so.main(SO.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke

However I'd like to catch that exception and add additional info to it, while still having the original stack trace.

For example, I'd like to have that:

Exception in thread "main" CustomException: / by zero (you tried to divide 42 by 0)
    at com.so.main(SO.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke

So basically I want to catch the ArithmeticException and rethrow, say, a CustomException (adding "you tried to divide 42 by 0" in this example) while still keeping the stacktrace from the original ArithmeticException.

What is the correct way to do this in Java?

Is the following correct:

try {
     ....
} catch (ArithmeticException e) {
     throw new CustomException( "You tried to divide " + x + " by " + y, e );
}
like image 682
NoozNooz42 Avatar asked Dec 31 '10 04:12

NoozNooz42


People also ask

What do I put in an exception message?

The content of the exception message dependents on the receiver of the message - so you have to put yourself in that person's shoes. A support engineer will need to be able to identify the source of the error as quickly as possible. Include a short descriptive string plus any data that can help troubleshoot the issue.

What is data exception?

If we find that something in your data is not correct and we can't start producing your order, we raise an “Exception”. Possible reasons are: the data is not complete – e.g. the drill file is missing. the data is ambiguous – e.g. top and bottom layers are not clearly defined.

What does throw new exception mean?

Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword.

How do I get exception details in python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.


2 Answers

You could also do this :

try {
     ....
} catch (ArithmeticException e) {
     ArithmeticException ae = new ArithmeticException( "You tried to divide " + x + " by " + y+" "+e.getMessage());
     ae.setStackTrace(e.getStackTrace());
     throw ae;
}

Which would give you "invisible" exceptions :-)

Update [27-september-2012] :

In Java 7: another cool trick is:

try {
    ...
} catch (ArithmeticException e) {
    e.addSuppressed(new ArithmeticException( "You tried to divide " + x + " by " + y));
    throw e;
}
like image 68
Pat Avatar answered Oct 24 '22 11:10

Pat


Yes, you can nest exceptions like that in Java, as of Java 1.4. I do this all the time. See http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Throwable.html.

When someone prints the stack trace from your custom exception, it'll show both the CustomException stack trace and the stack trace of the nested ArithmeticException. You can nest arbitrarily deeply.

like image 27
Brian Clapper Avatar answered Oct 24 '22 10:10

Brian Clapper