Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java thread exceptions

Tags:

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?

like image 845
Ammu Avatar asked Jul 12 '11 10:07

Ammu


People also ask

What is thread exception in Java?

Exceptions are objects that represent errors that may occur in a Java program. Java run system will throw IllegalThreadStateException whenever we attempt to invoke a method that a thread cannot handle in the given state.

How do you handle exceptions in threads?

Exception handling in Thread : By default run() method doesn't throw any exception, so all checked exceptions inside the run method has to be caught and handled there only and for runtime exceptions we can use UncaughtExceptionHandler.

What happens when an exception occurs in a thread?

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.


2 Answers

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.

like image 100
zacheusz Avatar answered Oct 14 '22 19:10

zacheusz


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

like image 22
Andrzej Doyle Avatar answered Oct 14 '22 18:10

Andrzej Doyle