throw statement in Java: How it works, Syntax and Examples

throw keyword is used to throw the exception manually in Java.

After a throw is encountered, following things happen:

1) Program execution is stopped.

2) Nearest try block is checked for matching catch statements.

3) If no match is found, next try statement is checked.

4) If no match is found ever, then default exception handler of Java is used to take care of the exception.


throw Throwable_Instance;

Here, Throwable_Instance is an object having type Throwable or a subclass of Throwable class.

For Example:

class Test {

static void meth() {

try {

throw new NullPointerException(“me”);

} catch(NullPointerException e) {

System.out.println(“Caught inside meth”);

throw e;



public static void main(String args[]) {

try {


} catch(NullPointerException e) {

System.out.println(“Caught again: ” + e);





Caught inside meth.

Caught again: java.lang.NullPointerException: me

Leave a Reply