Java 范例 - 多线程异常处理
下面的范例演示了多线程异常处理方法
class MyThread extends Thread{ public void run(){ System.out.println("Throwing in " +"MyThread"); throw new RuntimeException(); } } class Main { public static void main(String[] args){ MyThread t = new MyThread(); t.start(); try{ Thread.sleep(1000); } catch (Exception x){ System.out.println("Caught it" + x); } System.out.println("Exiting main"); } }
编译运行以上 Java 代码,输出结果如下
Throwing in MyThread Exception in thread "Thread-0" java.lang.RuntimeException at testapp.MyThread.run(Main.java:19) Exiting main