Java サンプル - マルチスレッド例外処理

Java 实例Java サンプル

以下のサンプルは、マルチスレッドの例外処理方法を示しています:

Main.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"); } }

上記のコードを実行した出力結果は次のとおりです:

Throwing in MyThread
Exception in thread "Thread-0" java.lang.RuntimeException
        at testapp.MyThread.run(Main.java:19)
Exiting main

Java 实例Java サンプル

その他の拡張