6

I have a not small multiple threads application with GUI and socket communications. During the development, I found sometimes there are some exceptions are not caught and logged. I have to stare at the console to get them if there is any.

Is there a way to catch those uncaught exceptions from different threads (including EDT) in one place, saying in main(), and log them? I do put a try-catch in main() to catch the Throwable but it doesn't work.

EDIT:

More specific, I have Executors.newCachedThreadPool() with Runnables. I don't want to use Callable in many cases because I don't want to block my calling thread. Then how can I catch exceptions from those Runnables?

And also how can I catch uncaught exception from the Swing EDT?

peterboston
  • 877
  • 1
  • 12
  • 24
  • Use a Thread.UncaughtExceptionHandler like in [this question](http://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread) – DaniEll Dec 10 '15 at 13:18
  • 1
    uhm ... why do you think that `Callable` blocks the "calling thread"? That doesnt even make any sense, its like saying that bananas cause global warming because of their taste .... `Callable` is a `functional interface` and can be applied to many method signatures - this has nothing to with blocking operations .... at all. `Callable` only blocks if the caller doesnt use threads - exchanging the callable for a runnable wont change that. – specializt Dec 10 '15 at 15:42

2 Answers2

7

I would propose to set a custom handler of type UncaughtExceptionHandler for non-caught exceptions using method Thread.setDefaultUncaughtExceptionHandler. This handler will be invoked by JVM when thread is about to terminate due to an uncaught exception.

    Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
            System.out.println(t + " throws exception: " + e);
    });

UPD:

As for Swing EDT case, I think there is nice answer here.

Community
  • 1
  • 1
nogard
  • 9,432
  • 6
  • 33
  • 53
  • I got your solution for some of my Threads. But I also have a thread pool and the Swing EDT. How to handle them? Please see my edit. – peterboston Dec 10 '15 at 15:08
-1

Not a simple problem in a large complex program because there is no way to catch an exception in a different thread from the one that threw it. You will have to make sure that every thread in the program has a handler that will catch all exceptions, and report them.

That's easy enough if you control the code that creates all of the threads, but harder if you call library routines that create threads on your behalf. If you're lucky, the libraries will let you provide a ThreadFactory, thus allowing your code to gain control whenever they create a new thread.

Even if you can make sure that every thread has an uncaught exception handler that does the right thing, you still might have some code hiding somewhere (maybe in some 3rd-party library that you call) that catches an exception and ignores it.

Good luck!

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • 1
    thats .... simply untrue. I recommend not writing answers about topics which are alien to you. Exceptions propagate / bubble up and it is in fact extremely easy to catch everything, see the first comment under the question. – specializt Dec 10 '15 at 15:46
  • 2
    @specializt It's not so easy to catch an exception when it "bubbles up" to code that is not yours, and it's not so easy to catch an exception when it is caught and ignored in code that is not yours. – Solomon Slow Dec 10 '15 at 16:59
  • thats a COMPLETELY different story which has nothing to do with normal, uncatched exceptions. – specializt Dec 11 '15 at 09:58