如何抛出 RuntimeException(“找不到符号”)
我试图在我的代码中抛出一个异常,如下所示:
throw RuntimeException(msg);
但是当我在 NetBeans 中构建时,我收到此错误:
C:\....java:50: cannot find symbol
symbol : method RuntimeException(java.lang.String)
location: class ...
throw RuntimeException(msg);
1 error
我需要导入某些内容吗?我拼写错误吗?我确信我一定做了一些愚蠢的事情:-(
I'm trying to throw an exception in my code like this:
throw RuntimeException(msg);
But when I build in NetBeans I get this error:
C:\....java:50: cannot find symbol
symbol : method RuntimeException(java.lang.String)
location: class ...
throw RuntimeException(msg);
1 error
Do I need to import something? Am I misspelling it? I'm sure I must be doing something dumb :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
throw new RuntimeException(msg);
您需要其中的
new
。它创建一个实例并抛出它,而不是调用一个方法。throw new RuntimeException(msg);
You need the
new
in there. It's creating an instance and throwing it, not calling a method.Exception
与 Java 中的任何其他对象一样都是一个Object
。您需要使用new
关键字创建一个新的异常
,然后才能抛出
它。您也可以选择执行以下操作:
两个代码片段是等效的。
链接到教程以确保完整性。
An
Exception
is anObject
like any other in Java. You need to use thenew
keyword to create a newException
before you canthrow
it.Optionally you could also do the following:
Both code snippets are equivalent.
Link to the tutorials for completeness.
正如其他人所说,在抛出对象之前先实例化该对象。
只是想添加一点;抛出 RuntimeException 的情况非常罕见。 API 中的代码抛出此异常的子类是正常的,但通常应用程序代码会抛出 Exception,或者扩展 Exception 但不扩展 RuntimeException 的内容。
回想起来,我错过了添加为什么使用 Exception 而不是 RuntimeException 的原因; @Jay,在下面的评论中添加了有用的位。 RuntimeException 不是受检查异常;
As everyone else has said, instantiate the object before throwing it.
Just wanted to add one bit; it's incredibly uncommon to throw a RuntimeException. It would be normal for code in the API to throw a subclass of this, but normally, application code would throw Exception, or something that extends Exception but not RuntimeException.
And in retrospect, I missed adding the reason why you use Exception instead of RuntimeException; @Jay, in the comment below, added in the useful bit. RuntimeException isn't a checked exception;
你必须在抛出它之前实例化它
PS:
有趣的是,Netbeans IDE 应该已经指出了编译时错误
you will have to instantiate it before you throw it
PS:
Intrestingly enough the Netbeans IDE should have already pointed out that compile time error
您需要使用
new
创建 RuntimeException 的实例,就像创建大多数其他类的实例一样:You need to create the instance of the RuntimeException, using
new
the same way you would to create an instance of most other classes:使用
new
关键字总是会创建一个实例(新对象)并抛出它,而不是调用方法使用
new
关键字内存清理[因为使用和抛出]:Using the
new
keyword will always create an instance (new object) and throw it, not called a methodUsing
new
keyword memory clean [because use and throw]:只是对于其他人:确保它是 new RuntimeException,而不是需要 error 作为参数的 new RuntimeErrorException。
Just for others: be sure it is new RuntimeException, not new RuntimeErrorException which needs error as an argument.
与任何其他异常不同,我认为 RuntimeException 是唯一一个不会停止程序但它仍然可以继续运行并恢复的异常,只需打印出一堆异常行?如果我错了请纠正我。
unlike any other Exceptions I think RuntimeException is the only one that will not stall the program but it can still keep running and recover just print out a bunch of Exception lines? correct me if I am wrong.