Wednesday, 1 March 2017

JAVA : EXCEPTION : Custom Exception

JAVA : EXCEPTION : Custom Exception



public class MyException extends Exception
{
    public MyException(String mymsg)
    {
        super(mymsg);
    }

}



class MyException extends Exception{
    String str1;
    MyException(String str2) {
       str1=str2;
    }
    public String toString(){
       return ("Output String = "+str1) ;
    }

}



good design/practice

Raising custom exceptions also allows you to treat exceptions in a more precise manner; for instance, if you have defined FooException inheriting IOException, then you can have a special treatment for it:


try { ... }
catch (FooException e) { ... } // Catch it _before_ IOException!
catch (IOException e) { ... }










No comments:

Post a Comment