Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0105 Creates and throws an exception

The handler that catches the exception rethrows it to the outer handler. public class Main { static void aMethod() { try { throw new NullPointerException("demo"); } catch (NullPointerException e) { System.out.println("Caught inside demoproc."); throw e; // rethrow the exception } } public static void main(String args[]) { try { aMethod(); } catch (NullPointerException e) { System.out.println("Recaught: " + e); } } }