gpt4 book ai didi

java - SCJP:未捕获异常后程序未终止

转载 作者:行者123 更新时间:2023-11-29 06:14:25 27 4
gpt4 key购买 nike

public class Threads2 implements Runnable {

public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}

public static void main(String[] args) {
Thread t = new Thread(new Threads2());
t.start();
System.out.println("End of method.");
}
}

我预测输出为

run.
//exception

但它显示输出为,

run
exception
end of method

(或)

run
end of method
exception

我想知道,一旦发生异常,程序就会终止,对吗?

最佳答案

不,您的程序不会终止,但线程会终止。

当一个线程抛出一个未捕获的异常时它终止。您的 main 线程继续运行。

关于java - SCJP:未捕获异常后程序未终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com