JAVA Program which Accept Age and if it is less than 18 then Throw Exception

JAVA Program which Accept Age and if it is less than 18 then Throw Exception:


 import java.io.*;
 class Not_eligible_driving extends Exception
 {
   Not_eligible_driving(String s)
   {
     super(s);
   }
 }

 class driving
 {
  public static void main(String args[])
  {
   try
   {
    int age;
    age=Integer.parseInt(args[0]);
    System.out.println("Entered Age is: "+age);
    if(age<18)
     throw new Not_eligible_driving("Not valid age");
    else
     System.out.println("Eligible for driving");
   }
   catch(Not_eligible_driving e)
   {
       System.out.println(e);
   }
  }
 }

OUTPUT:

Entered Age is: 17
Not_eligible_driving: Not valid age

Entered Age is: 25
Eligible for driving



Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen − 8 =