User Defined Exception or custom exception is creating user own exception class and throws that exception using ‘throw’ keyword. User-defined exception must extend Exception class.
In order to create custom exception, it is needed to extend Exception class that belongs to java.lang package.
Ex:
class JavaException{
public static void main(String args[]){
try{
throw new MyException(2);
// throw is used to create a new exception and throw it.
}
catch(MyException e){
System.out.println(e) ;
}
}
}
class MyException extends Exception{
int a;
MyException(int b) {
a=b;
}
public String toString(){
return ("Exception Number = "+a) ;
}
}
0 comments:
Post a Comment