class FuShuException extends Exception{
private int value;
FuShuException(String msg,int value){
super(msg);
this.value = value;
}
public int getValue(){
return value;
}
}
class ExceptionDemo{
public static void main(String[]args){
Demo d = new Demo();
try{
System.out.println(d.div(2,0));
}
//捕捉自定义异常,除数为负数
catch(FuShuException e ){
System.out.println(e.toString());
System.out.println(e.getValue());
}
//捕捉除数为0的异常
catch(Exception e){
System.out.println(e.toString());
}
}
}
class Demo{
public int div(int a,int b) throws FuShuException
{
if(b<0){
throw new FuShuException("出现负数!",b);
}
return a / b;
}
}
转载于:https://blog.51cto.com/9274590/1762906