0

我的一个朋友开始在他的 Java 项目中使用异常。由于未捕获的异常终止了程序,他基本上将所有异常转换为断言。因此,他可以在客户站点关闭所有异常。(实际上,他不需要禁用它们,而是必须在开发期间使用-ea/启用它们-enableassertions。)

class Logger {
    public static void logException(Throwable e) {
    assert false : e.getMessage();
    }
}

这是在客户站点禁用异常的合理方法吗?有哪些替代方案,他的方法有哪些重要缺点?

Please note that he is very determined at disabling exceptions at the customer. (Therefore answers of the type: "Do not disable exceptions at the customer." will not be helpful to me, but maybe to other people having a similar problem.)

4

1 回答 1

2

For the record, let me start by saying that you should never ever ever blindly disable runtime exceptions. There is a reason why these terminate your program. Java has entered a WTF brain-fart bad state and wants to get you out as safely as possible. Something is clearly wrong with your program and the sooner you know about it the better. The last thing you want is your customer going along thinking that everything's peachy while their program is crapping all over itself.

我认为处理此问题的正确方法是在各个级别捕获您的异常并提供有效的替代方案。也许您可以放弃工作并重新提交。也许您向用户提供了一个错误,该错误将通知他们该问题(并可能帮助他们下次避免该问题)。这里的重点是您可能不应该首先发生完全未捕获的(运行时)异常;在您的代码中的某个地方,并且希望在多个逻辑级别上,您应该尽可能以编程方式优雅地处理这些异常。

于 2013-03-30T10:14:05.237 回答