【发布时间】:2010-02-23 08:46:26
【问题描述】:
在一种方法中多次尝试并像这样构造代码是否被认为是不好的做法?
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
}
}
【问题讨论】:
-
为什么应该是一个不好的做法?似乎是有序的(我的意思是,你只在必须的地方赶上)。我唯一会考虑的是在
whatever之外抛出异常,因为我更喜欢抛出异常并越早看到错误越好(只在表示层捕获异常)。 -
如果不了解更多细节,就无法回答这个问题。如果 IOException 可以恢复,那就是好风格,如果不能,那就是非常糟糕的风格。
-
IOException 只是一个例子。我想知道一种方法中的多次尝试。
标签: java exception coding-style