Java Exception catch sequence

Question

What is the output of the following code?

public class Main {
  public static void main(String args[]) {
    try {/*from   w  ww  . j  a  v  a2 s. com*/
      int a = 0;
      int b = 42 / a;
    } catch (Exception e) {
      System.out.println("got one");
    } catch (ArithmeticException e) {
      System.out.println("ArithmeticException");
    }
  }
}


Compile time error
Unreachable catch block for ArithmeticException. 
It is already handled by the catch block for Exception

Note

A subclass must come before its superclass in a series of catch statements.

If not, unreachable code will be created and a compile-time error will result.




PreviousNext

Related