Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercepting exceptions

I'd like to use to a custom exception to have a user-friendly message come up when an exception of any sort takes place.

What's a good straightforward way of doing this? Are there any extra precautions I should take to avoid interfering with Swing's EDT?

like image 616
James P. Avatar asked Jul 04 '10 14:07

James P.


People also ask

What are the three types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

What are the two types of exceptions?

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception.

What do you mean by catching an exception?

When an appropriate handler is found, the runtime system passes the exception to the handler. An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler. The exception handler chosen is said to catch the exception.

What are the types of exceptions and give examples?

Common checked exceptions include IOException, DataAccessException, InterruptedException, etc. Common unchecked exceptions include ArithmeticException, InvalidClassException, NullPointerException, etc. 6. These exceptions are propagated using the throws keyword.


1 Answers

Exception Translation:

It's a good idea to not pollute your application with messages that have no meaning to the end user, but instead create meaningful Exceptions and messages that will translate the exception/error that happened somewhere deep in the implementation of your app.

As per @Romain's comment, you can use Exception(Throwable cause) constructor to keep track of the lower level exception.

From Effective Java 2nd Edition, Item 61:

[...] higher layers should catch lower-level exceptions and, in their place, throw exceptions that can be explained in terms of the higher-level abstraction. This idiom is known as exception translation:

   // Exception Translation
    try {
         // Use lower-level abstraction to do our bidding
         ...
    } catch(LowerLevelException e) {
         throw new HigherLevelException(...);
    }
like image 176
bakkal Avatar answered Oct 15 '22 20:10

bakkal