June 2018
Beginner
722 pages
18h 47m
English
When an exception is thrown inside a try block, it redirects control flow to its first catch clause (the one that catches NullPointerException in the following example):
void exceptionCaught(){ try { method2(); } catch (NullPointerException ex){ System.out.println("NPE caught"); ex.printStackTrace(); } catch (RuntimeException ex){ System.out.println("RuntimeException caught"); ex.printStackTrace(); } catch (Exception ex){ System.out.println("Exception caught"); ex.printStackTrace(); }}
If there are more than one catch clauses, the compiler forces you to arrange them so that the child exception is listed before the parent exception. In our previous example, NullPointerException extends RuntimeException extends Exception. If the ...
Read now
Unlock full access