Can We Write Code After Throw Statement in Java



No, we can not place any code after throw statement, it leads to compile time error. The compiler will show this error as unreachable statement. In Java, the throw statement immediately terminates the current flow of execution, therefore, the code immediate to a throw statement will not be executed. The control is then transferred to the next catch block or the caller method.

The throw Keyword in Java

The throw keyword is used to throw an exception manually. Whenever it is required to stop the execution of the functionality based on the user-defined logical error condition, we will use this throw keyword to throw an exception. We need to handle these exceptions using try and catch blocks.

Rules to use throw Keyword

Following rules must be adhere when you are using throw keyword in your Java program:

  • The throw keyword must follow Throwable type of object.
  • It must be used only in the method logic.
  • Since it is a transfer statement, we cannot place statements after throw statement. It leads to a compile-time error as mentioned earlier.
  • We can throw user-defined and predefined exceptions using throw keyword.

Example: Use of throw Keyword

In the given example, we are using throw keyword for throwing ArithmeticException.

public class Example {
   public static void main(String[] args) {
      try {
         throw new ArithmeticException();
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}

The above code displays the following output:

java.lang.ArithmeticException

Example: Code after throw Keyword

The following Java program shows what will happen if you write any code after the throw keyword:

public class ThrowKeywordDemo {
   public static void main(String[] args) {
      try {
         throw new ArithmeticException();
		 // compile-time error, unreachable statement
         System.out.println("In try block"); 
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}

The above code doesn't execute because there is a statement after a throw statement in the try block, which can cause a compile-time error:

ThrowKeywordDemo.java:6: error: unreachable statement
   System.out.println("In try block"); 
   ^
1 error

Example: throw Keyword in Conditional Block

If the throw statement is placed inside a conditional block, and the condition is not met, the code after the throw statement will be executed as shown in the Java program below:

public class ThrowKeywordDemo {
   public static void main(String[] args) {
      try {
         if (false) {
            throw new RuntimeException();
         }
         System.out.println("In try block"); 
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}

Here, the code after the throw statement will run because the condition is false, which prevents the throw statement from being executed. Hence, the program will continue to the next line.

In try block
Updated on: 2025-05-16T18:29:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements