How to Use Throw Statement in Java


In this example we will show you how to use the throw statement in Java.

Source Code

package com.beginner.examples;

public class Throw { 
 
  public static void main(String[] args) {
    int i =1;
    if(i > 0) {
      throw new ArithmeticException(">0"); // use the throw statement
    } else {
       System.out.println("OK"); 
    }
  }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: >0
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments