How to
catch an exception thrown by run() of a running thread?
Below line set UncaughtExceptionHandler concrete class to catch
Exception. Concrete class include definition of exception handler.
setDefaultUncaughtExceptionHandler
is a
static method of Thread class.
Thread.setDefaultUncaughtExceptionHandler(new
ThreadExceptionHandler());
Interface
used
@FunctionalInterface
public interface
UncaughtExceptionHandler {
/**
* Method invoked when the given thread
terminates due to the
* given uncaught exception.
* <p>Any exception
thrown by this method will be ignored by the
* Java Virtual Machine.
* @param t the thread
* @param e the
exception
*/
void uncaughtException(Thread t, Throwable e);
}
Concrete class implementing UncaughtExceptionHandler
import
java.lang.Thread.UncaughtExceptionHandler;
/* Method invoked when the given thread
terminates due to the
given uncaught exception.*/
public class
ThreadExceptionHandler implements UncaughtExceptionHandler{
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Thread
" + t.getName() + "Throwable " + e);
throw new
NullPointerException();
}
}
ThreadException client class
package
com.threadexception;
/* How to
catch an exception thrown
* by run() of a running thread */
public class ThreadException
{
public static void main(String[] args) {
Thread t1 = new Thread(new Task());
System.out.println("Thread
starts " +
Thread.currentThread().getName());
Thread.setDefaultUncaughtExceptionHandler
(new ThreadExceptionHandler());
try {
t1.start();
} catch
(ArithmeticException e) {
System.out.println("Excption
Catch");
}
System.out.println("Main
Thread Ends");
}
}
class Task implements Runnable {
public void run() {
int a = 10 / 0;
}
}
Output
Thread
starts main
Main
Thread Ends
Thread
Thread-0Throwable java.lang.ArithmeticException: / by zero
Summary:
a) Ideally one default
implementation is giving for system wide if there is any thread which throws
any exception and other exception handler for particular threads.
b)While using executorService.submit() or executorService.execute(), always remember that execute() returns nothing and thus
it throws Exception where as submit() returns Future and until we’re not
getting future.get(), it doesn’t throw any Exception.
c) By implementing ThreadFactory, we can rule over threads
created by ThreadPool.
Using ThreadFactory
/* An object
that creates new threads on demand */
public class SpecialThread implements ThreadFactory {
/* Adding special property to all
threads being created
* No
need to set default Exception handler
*/
public Thread
newThread(Runnable r) {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler(new
ThreadExceptionHandler());
return t;
}
}
/* How to
catch an exception thrown
* by run() of a running thread */
public class ThreadException
{
public static void main(String[] args) {
/* Creating threads which auto-catch
the exception*/
ExecutorService executorService = Executors.
newCachedThreadPool(new
SpecialThread());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.shutdown();
}
}
class Task implements Runnable {
public void run() {
int a = 10 / 0;
}
}
No comments:
Post a Comment