package exception;
public class Exception {
public static void main(String[] args) {
// TODO Auto-generated method stub
Exception tct =new Exception();
int result = 0;
result = tct.test1();
System.out.println("result= "+result);
}
public int test1(){
int div = 10;
int result = 100;
try{while(div>-1)
{div--;
result= result + 100/div;}
return result;
}catch(Exception e){
e.printStackTrace();
System.out.println("捕获异常");
return -1;
}
}
}
问题出在了catch(Exception e),编译器错误的提示信息是;No exception of type Exception can be thrown; an exception type must be a subclass of Throwable。
我也试了下,应该是你在同一个包里面建了两个类,你把这个程序放在另外一个包里面试一下
提示说的很明确了:No exception of type Exception can be thrown; an exception type must be a subclass of Throwable。
异常类必须继承
public class Exception extends Throwable {