Ammu
Ammu

Reputation: 5137

java thread exceptions

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?

Upvotes: 27

Views: 32823

Answers (6)

zacheusz
zacheusz

Reputation: 8842

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.

Upvotes: 40

Andrzej Doyle
Andrzej Doyle

Reputation: 103837

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

Upvotes: 7

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

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

Upvotes: 0

biziclop
biziclop

Reputation: 49804

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.

Upvotes: 3

Daniel
Daniel

Reputation: 28

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

Upvotes: 0

Marek Sebera
Marek Sebera

Reputation: 40681

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?

Upvotes: 0

Related Questions