Explain Codes LogoExplain Codes Logo

Java exception not caught?

java
exception-handling
try-catch-finally
suppressed-exceptions
Nikita BarsukovbyNikita Barsukov·Jan 27, 2025
TLDR

Ensure your catch block matches the specific exception you're encountering. If an ArithmeticException occurs, your catch must be for ArithmeticException or a superclass such as Exception. See the example below:

try { // Risky division, hold onto your hats! int result = 10 / 0; } catch (ArithmeticException e) { // Exception caught and handled, phew! System.err.println("My math teacher always told me division by zero is a no-no. She was right!"); }

This example specifically addresses catching the ArithmeticException, making debugging more precise and manageable.

Handling multiple exceptions

In cases where you have nested try-catch-finally blocks and both the catch and finally blocks throw exceptions, Java prioritizes the exception from the finally block. This is specified in the Java Language Specification (Section 14.20.2). The exception thrown in the catch block, therefore, will be suppressed in favor of the one from the finally block.

Block execution order

Bear in mind the order of your try-catch-finally blocks. Regardless of whether an exception occurs in the try or catch block, the finally block always executes.

Unveiling suppressed exceptions

Don't forget to use Throwable.getSuppressed to discover suppressed exceptions. This is particularly useful in debugging scenarios where exceptions might be swallowed.

Active exception rule

Take note that Java's propagation rules permit for one active exception only. If multiple exceptions occur, all other exceptions but one should be either handled or logged to avoid losing valuable debugging information.

Throwing an exception

Remember, an exception thrown won't output any print statements. Ensure that the throw statement and subsequent print messages don't contradict the flow; this can help to explain the absence of expected output.

Javadoc as your guide

When in doubt, refer to Javadoc exception suppression examples as they provide comprehensive examples that can ease the understanding of complex behavior.

Nested try-catch blocks

Keep in mind that with nested try-catch blocks, a catch block can transfer control to an outer catch if the exceptions are properly chained and caught respectively.

Practical Tips in Handling Exceptions

1. The Precious finally

Remember, the finally block is very precious. When both catch and finally blocks throw exceptions, the Java machine throws the one from the finally block and suppresses the one from the catch block.

2. Make your catch precise

As much as possible, catch exceptions precisely. It's not always wise to use a generic Exception in the catch block when you precisely know the type of exception to handle.

3. Be resourceful with try-with-resources

The use of try-with-resources is always a good practice as it often results in cleaner code, reduces the chances of error hiding, and automatically handles exception suppression.