不能丟擲RuntimeException型別的異常
不能丟擲RuntimeException型別的異常
public class RuntimeException {
private static void throwit() throws Exception
{
System.out.print("throwit ");
//throw new Exception();
throw new RuntimeException(); /* eclipse中提示這句程式碼有誤,給出的錯誤提示是“No exception of type RuntimeException can be thrown; an exception type must be a subclass of Throwable” */
}
public static void main(String[] args) {
try
{
System.out.print("try ");
throwit();
}
catch(Exception e)
{
System.out.print("catch ");
}
finally
{
System.out.print("finally ");
}
System.out.println("last");
}
}
上述程式中,程式碼 throw new RuntimeException(); 在 eclipse中提示這句程式碼有誤,給出的錯誤提示是“No exception of type RuntimeException can be thrown; an exception type must be a subclass of Throwable”
網上檢視到可以丟擲RuntimeException型別的異常,到我這怎麼就出錯了,求高手解答,謝謝!
Spring 事物丟擲Exception 異常時事物沒有回滾
java重寫方法後丟擲的異常及常見RuntimeException
javaSE 異常,執行時異常,RuntimeException (必須停止程式碼執行,提醒程式設計師修改程式碼)
huntor10-16 13:46
等級 1樓
方法宣告中throws出的是checked exception。
RuntimeException及其子類是non-checked exception。
private static void throwit() throws Exception
=>
private static void throwit()
去掉這個
面試題4———請寫出5種常見到的runtime exception
ticmy10-16 13:59
等級 2樓
LZ啊,你自己的類名就叫RuntimeException,你這個類又不是Throwable的子類,怎麼作為異常丟擲
你要不肯改名,就試試這樣:
new java.lang.RuntimeException()
shaxiaozilove10-16 20:26
等級 3樓
引用 2 樓 的回覆:
LZ啊,你自己的類名就叫RuntimeException,你這個類又不是Throwable的子類,怎麼作為異常丟擲
你要不肯改名,就試試這樣:
new java.lang.RuntimeException()
問題確實出在這裡,沒注意到這個問題,謝謝。
shaxiaozilove10-16 20:27
等級 4樓
引用 1 樓 的回覆:
方法宣告中throws出的是checked exception。
RuntimeException及其子類是non-checked exception。
private static void throwit() throws Exception
=>
private static void throwit()
去掉這個
我試過了,這個去不去掉都沒影響的,謝謝。