Uncaught Exceptions in run method of any thread(runnable) causes thread to die off. Here is mechanism to handle these exceptions.
Java provides a way to register exception handler which is called in the case our run method has caused an exception and does not handles it. A very simple example on this is as follows :
Following is a simple handler code
package thread.exceptionHandling;
import java.lang.Thread.UncaughtExceptionHandler;
public class ExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable exception) {
System.out.println(thread + " \n " + exception);
}
}
Here, our handler class implements UncaughtExceptionHandler, which is a static inner interface inside Thread class. detail :- http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html
Following are the class implementing Runnable and the main class which make this class run.
package thread.exceptionHandling;
public class Consumer implements Runnable{
@Override
public void run() {
for(int i = 0 ; i < 100 ; i++){
System.out.println("i : " + i);
if(i%10 == 9)
throw new NullPointerException("Dummy Null Pointer Exception");
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package thread.exceptionHandling;
public class MainClass {
public static void main(String[] args) throws InterruptedException {
Consumer consumer = new Consumer();
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
new Thread(consumer).start();
}
}
We can register the default uncaught exception for each thread too.
Java provides a way to register exception handler which is called in the case our run method has caused an exception and does not handles it. A very simple example on this is as follows :
Following is a simple handler code
package thread.exceptionHandling;
import java.lang.Thread.UncaughtExceptionHandler;
public class ExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable exception) {
System.out.println(thread + " \n " + exception);
}
}
Here, our handler class implements UncaughtExceptionHandler, which is a static inner interface inside Thread class. detail :- http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html
Following are the class implementing Runnable and the main class which make this class run.
package thread.exceptionHandling;
public class Consumer implements Runnable{
@Override
public void run() {
for(int i = 0 ; i < 100 ; i++){
System.out.println("i : " + i);
if(i%10 == 9)
throw new NullPointerException("Dummy Null Pointer Exception");
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package thread.exceptionHandling;
public class MainClass {
public static void main(String[] args) throws InterruptedException {
Consumer consumer = new Consumer();
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
new Thread(consumer).start();
}
}
We can register the default uncaught exception for each thread too.