为了账号安全,请及时绑定邮箱和手机立即绑定

写了个差不多的异常,为什么会报错?求大神解答

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。



正在回答

3 回答

我也试了下,应该是你在同一个包里面建了两个类,你把这个程序放在另外一个包里面试一下

0 回复 有任何疑惑可以回复我~
#1

海上钓鳌客 提问者

非常感谢!
2015-07-27 回复 有任何疑惑可以回复我~

提示说的很明确了:No exception of type Exception can be thrown; an exception type must be a subclass of Throwable。

异常类必须继承

public class Exception extends Throwable {

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

写了个差不多的异常,为什么会报错?求大神解答

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信