Please catch Throwable in a task submitted to a thread pool

Exceptions thrown in a task submitted to a thread pool will just disappear. You won’t even see the log.

You can catch java.lang.Exception and handle it. But to make things even better, please catch java.lang.Throwable instead.

In theory, it’s not recommended to catch a Throwable. But to be pragmatic, it’s important for you to see in log what happened. Otherwise, you may waste a lot of time to find the cause of a bug.

And non-Exception Throwable, i.e. java.lang.Error does happen. A common one is ExceptionInInitializerError , which happens when class initialization goes wrong.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.