读书人

不能抛出RuntimeException类型的错误

发布时间: 2012-11-10 10:48:51 作者: rapoo

不能抛出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()

读书人网 >J2SE开发

热点推荐