寫了個(gè)差不多的異常,為什么會(huì)報(bào)錯(cuò)?求大神解答
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),編譯器錯(cuò)誤的提示信息是;No exception of type Exception can be thrown; an exception type must be a subclass of Throwable。
2015-07-25
我也試了下,應(yīng)該是你在同一個(gè)包里面建了兩個(gè)類,你把這個(gè)程序放在另外一個(gè)包里面試一下
2015-07-24
提示說的很明確了:No exception of type Exception can be thrown; an exception type must be a subclass of Throwable。
異常類必須繼承
public class Exception?extends Throwable {