Java 实例 - 多线程异常处理
以下实例演示了多线程异常处理方法:
Main.java 文件
classMyThreadextendsThread{publicvoidrun(){System.out.println("Throwing in " +"MyThread");
thrownewRuntimeException();
}}classMain{publicstaticvoidmain(String[]args){MyThreadt = newMyThread();
t.start();
try{Thread.sleep(1000);
}catch(Exceptionx){System.out.println("Caught it" + x);
}System.out.println("Exiting main");
}}
以上代码运行输出结果为:
Throwing in MyThread Exception in thread "Thread-0" java.lang.RuntimeException at testapp.MyThread.run(Main.java:19) Exiting main