Tuesday 13 January 2015

UncaughtExceptionHandler in Java

If you don’t handle any run time exception in your program, then program terminates immediately by throwing stack trace.
 
public class UncaughtExceptionEx {

    public static void main(String args[]) {
        System.out.println("Going to throw run time exception");
        throw new RuntimeException("Runtime Exception");
    }
}

When you run above program, you will get output like below.

Going to throw run time exception
Exception in thread "main" java.lang.RuntimeException: Runtime Exception
        at UncaughtExceptionEx.main(UncaughtExceptionEx.java:6)
Java Result: 1


Java provides a mechanism to handle exceptions that are not handled by program implicitly. By using these handlers, you can take specific action for an exception at one place. I mean there is no need to handle exception every time. You can write one global exception handler, and all the unhandled exceptions are handled by this.

UncaughtExceptionHandler can be defined at three levels.

1.   By using Thread. setDefaultUncaughtExceptionHandler method.
2.   By using uncaughtException method of ThreadGroup.
3.   By using setUncaughtExceptionHandler method of a thread.

Uncaught exception is handled by thread first, next by Thread group and finally by default exception handler of thread.
  
1.By using Thread. setDefaultUncaughtExceptionHandler method.
public class UncaughtExceptionEx {

    public static void main(String args[]) {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("Handled by global Exception handler");
                System.out.println(e.getMessage());
            }
        });

        System.out.println("Going to throw run time exception");
        throw new RuntimeException("Runtime Exception");
    }
}


Output
Going to throw run time exception
Handled by global Exception handler
Runtime Exception


2.By using uncaughtException method of ThreadGroup
public class MyThreadGroup extends ThreadGroup {

    public MyThreadGroup(String name) {
        super(name);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable t) {
        System.out.println(t.getMessage());
        System.out.println("Handled by MyThreadGroup....");
    }
}

public class UncaughtExceptionEx {

    public static void main(String args[]) {
       MyThreadGroup group = new MyThreadGroup("group1");
       
       Thread t1 = new Thread(group, new Runnable(){
           public void run(){
               System.out.println("Inside run method");
               throw new RuntimeException("Runtime Exception");
           }
       });
       
       t1.start();
    }
}


Output
Inside run method
Runtime Exception
Handled by MyThreadGroup....


3.By using setUncaughtExceptionHandler method of a thread.
public class UncaughtExceptionEx {

    public static void main(String args[]) {

        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("Exception handled");
                System.out.println(e.getMessage());
            }
        });
        
        throw new RuntimeException("Runtime Exception");
    }
}

Output
Exception handled
Runtime Exception

No comments:

Post a Comment