gpt4 book ai didi

java - java中异常处理和finally阻塞的疑惑

转载 作者:行者123 更新时间:2023-12-01 04:45:57 30 4
gpt4 key购买 nike

你能说说怎么做吗?

代码:

public void main(String[] args) {
try {
//Some Exception throws here
}
catch(SomeException se) {
se.printStackTrace();
}
finally {
try {
//SomeException1 throws here
}
catch(SomeException1 se1) {
se.printStackTrace();
//Control is getting stop in this block itself but i wanna print the below statement
}

// I need to print this statement whatever exception occurs
System.out.println("End Program");
}
}

最佳答案

只需添加另一个 finally block

public void main(String[] args) {
try {

//Some Exception throws here

}
catch(SomeException se) {
se.printStackTrace();
}
finally {
try {

//SomeException1 throws here

}
catch(SomeException1 se1) {
se.printStackTrace();
}
finally {
System.out.println("End Program"); ----> I need to print this statement whatever exception occurs
}
}
}

或者,如果您知道只会处理您处理的异常,则可以完全删除 finally block 。

关于java - java中异常处理和finally阻塞的疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/536811/

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