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

相关文章:

  • 2021-11-11
  • 2021-05-22
  • 2022-12-23
  • 2021-08-19
  • 2021-12-03
猜你喜欢
  • 2021-09-09
  • 2022-12-23
  • 2021-09-22
  • 2022-02-18
  • 2021-11-25
  • 2021-06-30
相关资源
相似解决方案