Check out example codes for "java how to throw exception". It will help you in understanding the concepts better.

Code Example 1

/* In this program we are checking the Student age
 * if the student age<12 and weight <40 then our program 
 * should return that the student is not eligible for registration.
public class ThrowExample {
   static void checkEligibilty(int stuage, int stuweight){ 
      if(stuage<12 && stuweight<40) {
         throw new ArithmeticException("Student is not eligible for registration"); 
      else {
         System.out.println("Student Entry is Valid!!"); 

   public static void main(String args[]){ 
     System.out.println("Welcome to the Registration process!!");
     checkEligibilty(10, 39); 
     System.out.println("Have a nice day.."); 

Code Example 2

public class ThrowException{
  public static void main(String [] args) throws Exception{ 
    //throws Exception line is needed if not using try-catch block
    throw new Exception("Errmessage");

Learn ReactJs, React Native from