Handling uncaught runtime exception generated in run method using - setDefaultUncaughtExceptionHandler method in java


Contents of page :
  • What is setDefaultUncaughtExceptionHandler method?
  • setDefaultUncaughtExceptionHandler method features >
  • Program to demonstrate setDefaultUncaughtExceptionHandler method >
  • Output analyzation >


We can use setDefaultUncaughtExceptionHandler method which can handle uncaught unchecked(runtime) exception generated in run() method.


What is setDefaultUncaughtExceptionHandler method?
setDefaultUncaughtExceptionHandler method sets the default handler which is called when a thread terminates due to an uncaught unchecked(runtime) exception.


setDefaultUncaughtExceptionHandler method features >
  • setDefaultUncaughtExceptionHandler method sets the default handler which is called when a thread terminates due to an uncaught unchecked(runtime) exception.
  • setDefaultUncaughtExceptionHandler is a static method method, so we can directly call  Thread.setDefaultUncaughtExceptionHandler to set the default handler to handle uncaught unchecked(runtime) exception.
  • It avoids abrupt termination of thread caused by uncaught runtime exceptions.

     
Defining setDefaultUncaughtExceptionHandler method >
  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
   public void uncaughtException(Thread thread, Throwable throwable) {
   System.out.println(thread.getName() + " has thrown " + throwable);
   }
  });




Program to demonstrate setDefaultUncaughtExceptionHandler method >

class MyRunnable implements Runnable {
  
   String str;
         
          /*
          * method will terminate due to an uncaught unchecked(runtime) exception.
          */
     public void run() {
            /* String wasn't initialized, so performing any operation
          * on it will throw NullPointerException and it will caught by
          * default handler defined in main method.
          */
            str.equals("abc");
     }     
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
     public static void main(String[] args) {
  
   Thread thread1 = new Thread(new MyRunnable(),"thread-1");
  
   /*
     * setDefaultUncaughtExceptionHandler method sets the default handler
     * which is called when a thread terminates due to an
     * uncaught unchecked(runtime) exception.
     *         
     */
   Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
       public void uncaughtException(Thread thread, Throwable throwable) {
          System.out.println(thread.getName() + " has thrown " + throwable);
       }
   });
  
   thread1.start();
     }
}
/*OUTPUT
thread-1 has thrown java.lang.NullPointerException
*/


Output analyzation >
In the above program we have defined setDefaultUncaughtExceptionHandler method. And in run method  str wasn't initialized, calling str.equals("abc"); throwed NullPointerException and it was caught by default handler defined in main method.



RELATED LINKS>



Important Thread methods (salient features, usage with programs)>

Sleep() method in threads - 10 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Join() method - 10 salient features of join

Thread isAlive() method

interrupt() method of thread in java

holdsLock(object) method to find whether current thread holds the lock on monitor of specified object.

Thread priorities - setPriority() and getPriority() methods

Using Suspend and resume method in threads



Methods for Adding ShutdownHook and handling uncaught runtime exception >

Threads addShutdownHook method in java

Handling uncaught runtime exception generated in run method using - setDefaultUncaughtExceptionHandler method




eEdit
Must read for you :