Skip to content

Instantly share code, notes, and snippets.

@kamlesh0606
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save kamlesh0606/6f012ddc3f070e3839e5 to your computer and use it in GitHub Desktop.

Select an option

Save kamlesh0606/6f012ddc3f070e3839e5 to your computer and use it in GitHub Desktop.
How to catch an Exception from a thread
Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread th, Throwable ex) {
System.out.println("Uncaught exception: " + ex);
}
};
Thread t = new Thread() {
public void run() {
System.out.println("Sleeping ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted.");
}
System.out.println("Throwing exception ...");
throw new RuntimeException();
}
};
t.setUncaughtExceptionHandler(h);
t.start();
More Help :
http://www.scodden.net/2012/05/exception-handling-in-java-threads.html
http://blog.yohanliyanage.com/2010/09/know-the-jvm-1-uncaught-exception-handler/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment