Skip to content

What Happens when a thread doesn’t throw an exception?

I notice, in this javadoc, that an UncaughtExceptionHandler is used for when an exception occurs but is not caught. But, will that thread fail quietly? I guess so, because it is going about its business asynchronously, but I’m investigating a related issue with one of our processes, and am surprised at only being aware of this now, 10+ years into my career.



I finally got to the bottom of my specific problem. It was due to shoddy error handling, allowing the thread to fail quietly:

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {

            public void run() {
                try {
                    //do some stuff
                } catch (Exception e) {
                System.out.println("done-worker thead");
                throw new RuntimeException("purposeful!");
                (thread, throwable) -> System.out.println("main thread; uncaugh exception from worker threadt: " + throwable.getMessage()));
        System.out.println("done-main thead");

In doing “some stuff”, the application hit an OutOfMemoryError, which is not, strictly seaking, an Exception. Changing the above to catch(Throwable t), solved it.

User contributions licensed under: CC BY-SA
6 People found this is helpful