gpt4 book ai didi

java - 这种 Java 异常风格是不好的做法吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:28:57 27 4
gpt4 key购买 nike

在一个方法中进行多次尝试并像这样构造代码是否被认为是不好的做法?

public void whatever() {
try {
methodThatMayThrowIOException();
} catch(IOException io) {
// do something with exception here
}

// do more stuff here that won't throw exceptions

try {
methodThatMayThrowCustomException();
} catch(CustomException ce) {
// do something with custom exception here
}
}

最佳答案

如果该方法可以继续执行,即使发生指定的异常也没有任何问题,这应该没问题。

如果异常会导致方法中进一步的问题,我会让它冒泡。

关于java - 这种 Java 异常风格是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316769/

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