本帖最后由 廖智 于 2012-10-14 15:42 编辑
自己写的异常程序,看不出程序哪里出错了!为什么程序上面有个红色叉叉?
这是程序代码,下面的图片中有错误提示。
package itcast.p1;
public class ExceptionDemo5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Demo5 d = new Demo5();
int x = d.div(4,0);
System.out.println(x);
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
catch(FuShuException e){
System.out.println("除数是"+e.getNum());
}
}
}
class Demo5{
int a,b;
int div(int a,int b)throws ArithmeticException,FuShuException{
if(b==0)
throw new ArithmeticException("除数为零啦...");
if(b<0)
throw new FuShuException(b);
return a/b;
}
}
class FuShuException extends Exception{ //自定义负数异常
private int num;
FuShuException(){
super();
}
FuShuException(String message){
super(message);
}
FuShuException(int num){
this.num = num;
}
public int getNum(){
return num;
}
}
|
|