Question

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:

       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).

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.

Was it helpful?

Solution

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.

          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();
           }
        }

OTHER TIPS

Try to use e.printStackTrace(). getMessage() is a method of Throwable class (super class of all exceptions of Java) inherited by every exception class like ArithmeticException. The getMessage() method prints only the message part(If any message is available) of the output printed by object 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

As answered by Kostja here

The message and the stacktrace are two distinct pieces of information. While the stackstrace is mandatory, the message isnt. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top