aakash2310

Untitled

Feb 21st, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. class UserException
  2. {
  3.     public static void main(String args[])
  4.     {
  5.             check(5);
  6.             check(11);
  7.     }
  8.     static void check(int a)
  9.     {
  10.         try
  11.         {
  12.  
  13.             if(a>10)
  14.             {
  15.                 throw new MyException(a);
  16.             }
  17.             else
  18.             {
  19.                 System.out.println(a);
  20.             }
  21.         }
  22.         catch(MyException e)
  23.         {
  24.             System.out.println(e);
  25.         }
  26.     }
  27. }
  28. class MyException extends Exception
  29. {
  30.     int num;
  31.     MyException(int a)
  32.     {
  33.         num = a;
  34.     }
  35.     public String toString()
  36.     {
  37.         String s = num + "Can't be greater than 10";
  38.         return s;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment