The code below will throw a RuntimeException which will be caught by the first catch block that handles the RuntimeException. Then
why is it that the second catch block is attempting to handle the RuntimeException. Is it because a RuntimeException and an ArithmeticException are both unchecked exception?.

public class Tester {
public static void main(String[] args) {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.println("RuntimeException");
} catch (ArithmeticException e) {
System.out.println("ArithmeticException");
} catch (Exception e) {
System.out.println("Exception");
}
}
}

Recommended Answers

All 7 Replies

According to the API docs What class does ArithmeticException extend?

Considering that, do you expect that catch block ever to be reached?

It extends the RuntimeException class.

Okay? So would you expect the ArithmeticException catch block to ever be executed?

Okay. So what's wrong?

The try block, as posted, doesn't even compile, bacause, with the point we just illuminated above, the ArithmeticException block is an unreachable block of code which is, then, uncompilable.

So, what's the actual, compilable code look like, and what's your problem with it?

well im trying to go deep into understanding about exceptions.

Okay, but you claimed that the second block was catching the exceptions, and, in fact, the code doesn't even compile, so on what were you basing your supposition?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.