gpt4 book ai didi

java - 如果 sleep、join 函数抛出 Interrupted 异常,那么为什么 catch block 不捕获它们?

转载 作者:行者123 更新时间:2023-12-01 20:27:09 24 4
gpt4 key购买 nike

这是连接函数:

public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable(), "t1");
Thread t2 = new Thread(new MyRunnable(), "t2");
Thread t3 = new Thread(new MyRunnable(), "t3");

t1.start();

//start second thread after waiting for 2 seconds or if it's dead
try {
t1.join(2000);
} catch (InterruptedException e) {
System.out.println("In catch");
}
t2.start();

众所周知,我们必须将 join 函数(和 sleep 函数)放在 try-catch block 中,然后当 join 函数抛出 InterruptedException 时,为什么 In catch 不打印?其背后的逻辑是什么?

最佳答案

java中抛出的定义是它们可以抛出,而不是说它们必须抛出。

异常抛出和捕获机制的目的是让你在异常发生时能够处理它们,并不意味着它们总是会发生

关于java - 如果 sleep、join 函数抛出 Interrupted 异常,那么为什么 catch block 不捕获它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715717/

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