Sunday 31 March 2013

use of throw

// Demonstrate throw.
class Myjava {
static void demoproc() {
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 {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}




o/p

Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo