简体   繁体   English

无法显示捕获的异常(newSingleThreadScheduledExecutor)

[英]can't show caught exception (newSingleThreadScheduledExecutor)

for schedule executor: 对于计划执行者:

        executor = Executors.newSingleThreadScheduledExecutor();
        Runnable ppt = new Runnable() {
            public void run() {
                try {
                    processTask();
                } catch(Exception e) {
                    System.out.println(e.getMessage());
                    //need to be aware of this exception, no message is outputted
                }
            }
        };
        executor.scheduleWithFixedDelay(ppt, 0, 1000/20, TimeUnit.MILLISECONDS);

for processTask method: 对于processTask方法:

       private void processTask() {
           try {
             //task business logic
           } catch(SomeOtherException e) {
                 System.out.println(e.getMessage());
                //I want to be aware of this exception also
           }
        }

I know the task failed for a reason and I don't want it to continue after that point (I use executor.shutdown() to cancel it). 我知道任务由于某种原因而失败,并且我不希望在那之后继续执行(我使用executor.shutdown()取消了它)。

I just need to know what the error was when the exception is caught. 我只需要知道捕获异常时的错误是什么。 It doesn't seem to do it in the above method. 上面的方法似乎没有做到这一点。

Thanks in advance for any response. 预先感谢您的任何回复。

You are putting try catch block in process task also that's why any problem in that method will resolve there and if you call shutdown then control would not return to the above method. 您还将try catch块放入流程任务中,这就是为什么该方法中的任何问题都将在那里解决的原因,并且如果您调用shutdown,则控制权将不会返回上述方法。

          Runnable ppt = new Runnable() {
                public void run() {
                    try {
                    processTask();
                    } catch(Exception e) {
                        System.out.println(e.getMessage());

                    }
                }
            };
         executor.scheduleWithFixedDelay(ppt, 0, 1000/20, TimeUnit.MILLISECONDS);

In this example you will get '/ by zero exception' and then scheduler will shutdown. 在此示例中,您将获得“ /零例外”,然后调度程序将关闭。

       private static void processTask() {
           try {
             //task business logic
               int x=2/0;
           } catch(Exception e) {
                 System.out.println(e.getMessage());
                //I want to be aware of this exception also
                  executor.shutdown();
           }
        }

Try to use e.printStackTrace() . 尝试使用e.printStackTrace() getMessage() is a method of Throwable class (super class of all exceptions of Java) inherited by every exception class like ArithmeticException . getMessage()Throwable class (Java所有异常的超类getMessage()的方法,由每个异常类(如ArithmeticException )继承。 The getMessage() method prints only the message part(If any message is available) of the output printed by object e. getMessage()方法仅打印对象e打印的输出的消息部分(如果有可用消息)。 While printStackTrace() method prints full stack trace along with the line number where exception occurred with error message- See more at: http://way2java.com/exceptions/getmessage-printstacktrace/#sthash.QMvLohu3.dpuf 虽然printStackTrace()方法将打印完整的堆栈跟踪记录以及发生错误消息的发生异常的行号-有关更多信息,请参见: http : //way2java.com/exceptions/getmessage-printstacktrace/#sthash.QMvLohu3.dpuf

As answered by Kostja here 正如Kostja 在这里回答的

The message and the stacktrace are two distinct pieces of information. 消息和堆栈跟踪是两条截然不同的信息。 While the stackstrace is mandatory, the message isnt. 虽然stackstrace是必需的,但消息不是。 Most exceptions deliver a message, and it is the best practice, but some just don't and there's nothing to be done to fix it. 大多数异常都会传达信息,这是最佳实践,但有些例外却没有,并且没有任何工作可以解决。

For more information you can see similar queries here link-1 and link-2 . 有关更多信息,您可以在link-1link-2处看到类似的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM