package cn.itcast.io;
//异常处理第3中方式:自定义异常!
class FuShuException extends Exception{
private int value;
public FuShuException() {
super();
}
public FuShuException(String message,int value) {
super(message);
this.value = value;
}
public FuShuException(String message) {
super(message);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
class Demo3{
int div(int a,int b) throws FuShuException{
if(b<0){
throw new FuShuException("出现了除数是负数的情况---/by fushu",b);
}
return a/b;
}
}
public class ExceptionDemo3 {
public static void main(String[] args) {
Demo3 d = new Demo3();
try {
int x = d.div(4, -9);
System.out.println("x="+x);
} catch (FuShuException e) {
// TODO: handle exception
System.out.println(e.toString());
System.out.println("除数出现了负数");
System.out.println("错误的负数是:"+e.getValue());
}
}
}
b值显示不出来 |