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?
There are three types of exception—the checked exception, the error and the runtime exception.
There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked 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.
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.
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(...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With