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

来源:1-3 Java中通过案例学习 try...catch...finally

海上钓鳌客

2015-07-23 23:43

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。



写回答 关注

2回答

  • soputasmile
    2015-07-25 09:29:43
    已采纳

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

    海上钓鳌客

    非常感谢!

    2015-07-27 09:36:04

    共 1 条回复 >

  • 管理員
    2015-07-24 08:01:22

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

    异常类必须继承

    public class Exception extends Throwable {

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409780 学习 · 4339 问题

查看课程

相似问题