Last active
August 29, 2015 14:04
-
-
Save kamlesh0606/6f012ddc3f070e3839e5 to your computer and use it in GitHub Desktop.
How to catch an Exception from a thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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