java - How to catch an Exception from a thread -


i have java main class, in class, start new thread, in main, waits until thread dies. @ moment, throw runtime exception thread, can't catch exception thrown thread in main class.

here code:

public class test extends thread {   public static void main(string[] args) throws interruptedexception   {     test t = new test();      try     {       t.start();       t.join();     }     catch(runtimeexception e)     {       system.out.println("** runtimeexception main");     }      system.out.println("main stoped");   }    @override   public void run()   {     try     {       while(true)       {         system.out.println("** started");          sleep(2000);          throw new runtimeexception("exception thread");       }     }     catch (runtimeexception e)     {       system.out.println("** runtimeexception thread");        throw e;     }      catch (interruptedexception e)     {      }   } } 

anybody knows why?

use thread.uncaughtexceptionhandler.

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(); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -