Throw Exception In Java
Chapter:
Exception Handling
Last Updated:
15-07-2016 10:25:37 UTC
Program:
/* ............... START ............... */
public class JavaThrowExample {
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);
}
}
}
/* ............... END ............... */
Output
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
Notes:
-
throw keyword is used in Java to throw custom exception.
- Syntax : throw exception; eg: throw new IOException("Error ....)
Tags
Throw Statement, Java