Skip to content


UnhandledExceptionHandler in Java: how to catch uncaught exceptions

UncaughtExceptionHandler in Java

In this short tutorial, I’m going to describe how to handle uncaught exceptions in threads. This can be done by:

  • Using threadInstance.setUncaughtExceptionHandler() ─ for a specific thread
  • Overriding ThreadGroup’s uncaughtException() method ─ for a thread group
  • Using Thread.setDefaultUncaughtExceptionHandler() ─ for all threads

If you’re interested in how to implement this, press “read more”, I’ve prepared an example.

Read More →

Java exceptions handling order

Consider there is a method which can throw IOException and FileNotFoundException. Is there a difference in which order we catch these exceptions in a single try-catch block?
Will this code work correctly?

try {
    // potential FileNotFoundException or IOException
} catch (IOException e) {
    // handling IOException
} catch (FileNotFoundException e) {
    // handling FileNotFoundException

Read More →

Can I use try-finally block (without catch)?

Yes, you can use the try-finally construction. As a use case, we can try to connect to a database and finally we will close the connection:

Connection connection = null;

try {
    connection = Database.getConnection();
    // some operations
} finally {
    try {
        if (connection != null) {
    } catch(SomeException e) {
        // what to do

This code looks a bit redundant. But in Java 7 we can use a try-with-resources block to handle our resources closing in a more convinient way…

Read More →