aakash2310

Untitled

Feb 21st, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.util.*;
  2. class VoterException
  3. {
  4.     public static void main(String args[])
  5.     {
  6.          Scanner s = new Scanner(System.in);
  7.         System.out.print("Enter Age : ");
  8.         int age = s.nextInt();
  9.         check(age);
  10.     }
  11.     static void check(int a)
  12.     {
  13.         try
  14.         {
  15.  
  16.             if(a<18)
  17.             {
  18.                 throw new MyException(a);
  19.             }
  20.             else
  21.             {
  22.                 System.out.println("Your age is eligible for voting");
  23.             }
  24.         }
  25.         catch(MyException e)
  26.         {
  27.             System.out.println(e);
  28.         }
  29.     }
  30. }
  31. class MyException extends Exception
  32. {
  33.     int num;
  34.     MyException(int a)
  35.     {
  36.         num = a;
  37.     }
  38.     public String toString()
  39.     {
  40.         String s =  "Your age is not eligible for voting";
  41.         return s;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment