当前位置: 代码迷 >> J2SE >> 不能抛出RuntimeException类型的错误
  详细解决方案

不能抛出RuntimeException类型的错误

热度:7258   发布时间:2013-02-25 00:00:00.0
不能抛出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类型的异常,到我这怎么就出错了,求高手解答,谢谢!

------解决方案--------------------------------------------------------
方法声明中throws出的是checked exception。

RuntimeException及其子类是non-checked exception。

 private static void throwit() throws Exception
=>
 private static void throwit()
去掉这个
------解决方案--------------------------------------------------------
LZ啊,你自己的类名就叫RuntimeException,你这个类又不是Throwable的子类,怎么作为异常抛出

你要不肯改名,就试试这样:
new java.lang.RuntimeException()
  相关解决方案