26

Assume a thread is started from the main method. What happens if an exception is thrown in the thread but not handled within the thread?

Is it possible to propagate the exception back to the main method?

Matthew Walker
  • 2,527
  • 3
  • 24
  • 30
Ammu
  • 5,067
  • 9
  • 34
  • 34

6 Answers6

39

We are talking about unchecked exceptions thrown from Thread.run method. By default, you will get sth like this in system error:

Exception in thread "Thread-0" java.lang.RuntimeException
    at Main$1.run(Main.java:11)
    at java.lang.Thread.run(Thread.java:619)

This is the result of printStackTrace for unhandled exceptions. To handle it, you can add your own UncaughtExceptionHandler:

   Thread t = new Thread(new Runnable(){
        public void run() {
            throw new RuntimeException();
        }       
    });
   t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("exception " + e + " from thread " + t);
        }
    });
    t.start();

To set handler for all threads use a static method Thread.setDefaultUncaughtExceptionHandler.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • 1
    so to sum it up, it does propagate back to the main thread, and is automatically handled by default. is that right? – JackOuttaBox Nov 19 '19 at 06:30
7

If the exception is caught and handled by the code running in that thread, then it will be handled however the catch block logic is written. I'll assume for the rest of this answer that you're talking about uncaught exceptions.

An uncaught exception will cause the thread to exit. When it bubbles to the top of Thread.run() it will be handled by the Thread's UncaughtExceptionHandler. By default, this will merely print the stack trace to the console. The thread itself will exit at this point - it couldn't really continue anyway, because its run() method has finished.

So if you want the exception to be reraised in your main thread, you can define an UncaughtExceptionHandler that will do this (it's a very simple interface), and then call Thread.setUncaughtExceptionHandler on the spawned thread after it's created, passing in your custom exception handler.


The only potentially tricky part about writing the handler is determining where and how exactly you're going to "insert" the throwable into your Main thread. This isn't entirely obvious, if your thread is off doing something else, and will depend very much on how you've designed your application and what its concurrent support looks like.

(If, on the other hand your main thread is just waiting for the other Thread to run, then it gets easier. But in this case, perhaps your Main thread should be submitting a task to a threaded ExecutorService and blocking on a Future, which will handle all of this wiring/lifecycle stuff for you?)

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
3

No, it can't. When you think about it, the main() method might have finished days ago.

Any uncaught exception from a thread is propagated to the thread's UncaughtExceptionHandler. If there's none defined, it goes to the thread group's handler, if that isn't set either, it goes to the default handler.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • The first line in your answer about `days ago` was helpful to better understand the issue. In effect, multiple threads can act like separate processes that happen to (easily) share data. The exact same model can be built (with some difficultly) using separate processes and shared memory. – kevinarpe May 08 '15 at 04:26
0

If exception is not handled by try {} catch (Exception e){} then it is raised and thrown to function which called.

If it is separate standalone Thread, you cannot propagate it to object which is not parent of thread

Or some other info here: How to throw a checked exception from a java thread?

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
0

Normally, if you don't implement the exceptions yourself, when some exception happens, it close the execution.

Daniel
  • 30
  • 1
  • 6
0

The exception is caught by the UncaughtExceptionHandler. You can catch it there an propagate it back to the main thread if you want to.

Mathias Schwarz
  • 7,099
  • 23
  • 28