Java - exception Handling - user defined exception handling


// user defined exception

class userdefinedexception extends Exception
{
     int a;
    
     userdefinedexception(int b)
     {
          a = b;
     }
    
     public String toString()
     {
          return ("Exception Number =  "+a) ;
     }
}

class JavaException
{
     public static void main(String args[])
     {
          try
          {
              // throw is used to create a new exception and throw it.
              throw new userdefinedexception(2);
      
          }
          catch(userdefinedexception e)
          {
              System.out.println(e) ;
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu