Java 'throw' Keyword Tutorial

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exceptions.

Usage

The throw keyword is mainly used to throw custom exceptions.

throw new ArithmeticException("Person is not eligible to vote");

Example

public class TestThrow {
   static void validate(int age) {
     if(age < 18)
        throw new ArithmeticException("not valid");
     else
        System.out.println("welcome to vote");
   }
   
   public static void main(String args[]) {
      validate(13);
      System.out.println("rest of the code...");
  }
}