Java Throw Statement



throw Keyword :

  1. The throw keyword is used to explictily throw an exception.
  2. We can throw either checked or uncheked exception. The throw keyword is mainly used to throw custom exception.

Requirement :

We want to show an error message if the marks of student are below 75. We need to throw an error message – “Please reappear for exam”.

class StudentExcepeption{

   static void validateMarks(int age){
     if(marks < 75)
      throw new ArithmeticException("Reappear for exam");
     else
      System.out.println("Student is having Distinction");
   }
   
   public static void main(String args[]){
      validateMarks(71);
      System.out.println("Remaining code...");
  }
}