如何抛出 RuntimeException(“找不到符号”)

发布于 2024-09-12 10:58:27 字数 342 浏览 5 评论 0原文

我试图在我的代码中抛出一个异常,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小鸟爱天空丶 2024-09-19 10:58:27

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.

樱&纷飞 2024-09-19 10:58:27

Exception 与 Java 中的任何其他对象一样都是一个 Object。您需要使用new关键字创建一个新的异常,然后才能抛出它。

throw new RuntimeException();

您也可以选择执行以下操作:

RuntimeException e = new RuntimeException();
throw e;

两个代码片段是等效的。

链接到教程以确保完整性。

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.

throw new RuntimeException();

Optionally you could also do the following:

RuntimeException e = new RuntimeException();
throw e;

Both code snippets are equivalent.

Link to the tutorials for completeness.

删除→记忆 2024-09-19 10:58:27

正如其他人所说,在抛出对象之前先实例化该对象。

只是想添加一点;抛出 RuntimeException 的情况非常罕见。 API 中的代码抛出此异常的子类是正常的,但通常应用程序代码会抛出 Exception,或者扩展 Exception 但不扩展 RuntimeException 的内容。

回想起来,我错过了添加为什么使用 Exception 而不是 RuntimeException 的原因; @Jay,在下面的评论中添加了有用的位。 RuntimeException 不是受检查异常;

  • 方法签名不必声明可能引发 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;

  • 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.
家住魔仙堡 2024-09-19 10:58:27

你必须在抛出它之前实例化它

throw new RuntimeException(arg0) 

PS:
有趣的是,Netbeans IDE 应该已经指出了编译时错误

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

沫尐诺 2024-09-19 10:58:27
throw new RuntimeException(msg); // notice the "new" keyword
throw new RuntimeException(msg); // notice the "new" keyword
囚我心虐我身 2024-09-19 10:58:27

您需要使用 new 创建 RuntimeException 的实例,就像创建大多数其他类的实例一样:

throw new RuntimeException(msg);

You need to create the instance of the RuntimeException, using new the same way you would to create an instance of most other classes:

throw new RuntimeException(msg);
妄断弥空 2024-09-19 10:58:27

使用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 create an instance using new keyword and throwing it 

使用new关键字内存清理[因为使用和抛出]:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
    
            //do your work here..
        }
    }, 1000);

Using the new keyword will always create an instance (new object) and throw it, not called a method

    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 create an instance using new keyword and throwing it 

Using new keyword memory clean [because use and throw]:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
    
            //do your work here..
        }
    }, 1000);
盛装女皇 2024-09-19 10:58:27

只是对于其他人:确保它是 new RuntimeException,而不是需要 error 作为参数的 new RuntimeErrorException。

Just for others: be sure it is new RuntimeException, not new RuntimeErrorException which needs error as an argument.

給妳壹絲溫柔 2024-09-19 10:58:27
throw new RuntimeException(msg);

与任何其他异常不同,我认为 RuntimeException 是唯一一个不会停止程序但它仍然可以继续运行并恢复的异常,只需打印出一堆异常行?如果我错了请纠正我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文