Java 如何抛出 RuntimeException(“找不到符号”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3406219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to throw RuntimeException ("cannot find symbol")
提问by Greg
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:
但是当我在 NetBeans 中构建时,我收到此错误:
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 :-(
我需要导入一些东西吗?我拼错了吗?我确定我一定在做一些愚蠢的事情:-(
采纳答案by j flemm
throw new RuntimeException(msg);
throw new RuntimeException(msg);
You need the new
in there. It's creating an instance and throwing it, not calling a method.
你需要new
在那里。它正在创建一个实例并抛出它,而不是调用一个方法。
回答by naikus
throw new RuntimeException(msg); // notice the "new" keyword
回答by jjnguy
An Exception
is an Object
like any other in Java. You need to use the new
keyword to create a new Exception
before you can throw
it.
一种Exception
是Object
像任何其他Java中。您需要使用new
关键字来创建一个新的Exception
,然后才能throw
吧。
throw new RuntimeException();
Optionally you could also do the following:
(可选)您还可以执行以下操作:
RuntimeException e = new RuntimeException();
throw e;
Both code snippets are equivalent.
两个代码片段是等效的。
回答by RHSeeger
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
与创建大多数其他类的实例相同的方式来创建RuntimeException的实例:
throw new RuntimeException(msg);
回答by Sudhakar
you will have to instantiate it before you throw it
你必须在扔之前实例化它
throw new RuntimeException(arg0)
PS: Intrestingly enough the Netbeans IDE should have already pointed out that compile time error
PS:有趣的是,Netbeans IDE 应该已经指出编译时错误
回答by Dean J
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.
只是想补充一点;抛出 RuntimeException 异常罕见。API 中的代码抛出 this 的子类是正常的,但通常情况下,应用程序代码会抛出 Exception 或扩展 Exception 但不扩展 RuntimeException 的东西。
And in retrospect, I missed adding the reason whyyou use Exception instead of RuntimeException; @Jay, in the comment below, added in the useful bit. RuntimeException isn't a checked exception;
而在回想起来,我错过了增加的原因,为什么你使用异常,而不是RuntimeException的; @Jay,在下面的评论中,添加了有用的部分。RuntimeException 不是已检查的异常;
- The method signature doesn't have to declare that a RuntimeException may be thrown.
- Callers of that method aren't required to catch the exception, or acknowlege it in any way.
- Developers who try to later use your code won't anticipate this problem unless they look carefully, and it will increase the maintenance burden of the code.
- 方法签名不必声明可能会抛出 RuntimeException。
- 该方法的调用者不需要捕获异常,也不需要以任何方式承认它。
- 以后尝试使用您的代码的开发人员除非仔细查看,否则不会预料到这个问题,并且会增加代码的维护负担。
回答by dillip pattnaik
Just for others: be sure it is new RuntimeException, not new RuntimeErrorException which needs error as an argument.
仅适用于其他人:确保它是新的 RuntimeException,而不是需要错误作为参数的新 RuntimeErrorException。
回答by SSpoke
throw new RuntimeException(msg);
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.
与任何其他异常不同,我认为 RuntimeException 是唯一一个不会停止程序但它仍然可以继续运行并恢复的异常,只需打印出一堆异常行?如果我错了,请纠正我。
回答by Keshav Gera
using new keyword we always create a instance (new object) and throwing it , not called a method
使用 new 关键字我们总是创建一个实例(新对象)并抛出它,而不是称为方法
throw new RuntimeException("Your Message");
You need the new in there. It's creating an instance and throwing it, not calling a method.
int no= new Scanner().nextInt(); // we crate an instance using new keyword and throwing it
using new keyword memory clean [because use and throw]
使用新关键字 memory clean [因为使用和抛出]
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//do your work here..
}
}, 1000);