fork download
  1. public class Main implements Thread.UncaughtExceptionHandler {
  2.  
  3. public static void main(String[] args) {
  4. Main main=new Main();
  5. Thread childThread=new ChildThread();
  6. childThread.start();
  7. childThread.setUncaughtExceptionHandler(main);
  8. for(int i=0; i < 100; i ++) {
  9. try {
  10. Thread.sleep(100);
  11. //System.out.println("Main thread, loop "+i);
  12. }
  13. break;
  14. }
  15. }
  16. }
  17.  
  18. public void uncaughtException(Thread t, Throwable e) {
  19. System.out.println("We are in " + Thread.currentThread().getClass().getName() + " and this is correct!");
  20. System.out.println("Caught thread "+t.getClass().getName()+" - we're back in main thread! (this is fucking lie)");
  21. }
  22. }
  23.  
  24. class ChildThread extends Thread {
  25.  
  26. @Override
  27. public void run() {
  28. int j=0;
  29. while(true) {
  30. try {
  31. Thread.sleep(100);
  32. System.out.println("Child thread, loop "+j++);
  33. if(j>=10) {
  34. throw new RuntimeException("Test!");
  35. }
  36.  
  37. }
  38. break;
  39. }
  40. }
  41. }
  42. }
  43.  
Success #stdin #stdout 0.06s 380480KB
stdin
Standard input is empty
stdout
Child thread, loop 0
Child thread, loop 1
Child thread, loop 2
Child thread, loop 3
Child thread, loop 4
Child thread, loop 5
Child thread, loop 6
Child thread, loop 7
Child thread, loop 8
Child thread, loop 9
We are in ChildThread and this is correct!
Caught thread ChildThread - we're back in main thread! (this is fucking lie)