简体   繁体   中英

Catch RuntimeException

During coding I want to catch some Exceptions from Runnables.

The code I tried to catch an Exception with:

public void fooBar(){
    //nullable value
    final String foo = null;
    try{
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(foo == null){
                    //Only Exception that can be thrown from a Runnable
                    throw new RuntimeException("F.U.B.A.R.");
                }
            }
        }).start();
    }catch(Exception ex){
        //In my opinion the way to catch this Exception
        System.out.println(ex.getMessage());
    }
}

What I Expect to see as an output is:

The output I'm getting:

The reason I need this is to put this Exception into my own so I can work with it properly down the line.

Does anyone know how to get the expected output instead of my current?

Because constructing Runnable and executing .start() between try and catch - did not throw exception. Runtime exception raised later, when thread executing "run" method.

Your current catch block catches exceptions on the main thread while your exception is thrown on the other thread. It won't work. You have to

The problem is because of two threads.The exception of one thread cannot be caught by another.

In your case,you are trying to catch an exception which may occur when starting the thread by the main thread new Thread(new Runnable() {--- }).start();

You need to catch the exception inside the Thread-0

            @Override
            public void run() {
             try{
                if(foo == null){
                    //Only Exception that can be thrown from a Runnable
                    throw new RuntimeException("F.U.B.A.R.");
                }
               }catch(Exception ex){
               //In my opinion the way to catch this Exception
               System.out.println(ex.getMessage());
              }
            }

As many above/below me have pointed out you are attempting to catch an exception in one thread with a completely different thread, this means that the default exception handler is catching and handling it . You can change that if you wish, but it is generally a better idea to catch the exception in the thread you are working with and handle it there.

If you need the two threads to talk to each other then you have a few options, such as the spawned thread catching the exception and passing passing it over, to using an ExecutorService , calling get on the Future and seeing if it throws a ExecutionException .

You have mentioned that you are using TimerTask s. If you can move to ScheduledThreadPoolExecutor then you can use the Future method. If not then you are going to have to catch the exception the TimerTask and pass the fact if failed back to the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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