假设我有一个如下所示的服务:
public interface MyAccountService
{
boolean create( String user );
}
create 方法执行了一些更改,即(为了讨论):
- 将消息添加到队列中
- 将一行添加到多个表中
- 创建 LDAP 帐户等...
目前,我将所有错误消息折叠为单个 boolean 返回值。 现在,如果在内部出现错误,我将为支持团队记录这些错误。
例如用户创建失败的典型日志
按照以下(严格)顺序创建“alistair”帐户:
- 添加到表 Foo:成功
- 添加到表格栏:成功
- 添加到 LDAP:失败
- 添加到队列:成功
这样,技术支持人员就可以决定如何修复帐户。
设计系统的最佳实践是什么,以便我们可以轻松跟踪 交易的成功/失败(并手动修复)?正在返回 boolean 值并吞掉所有异常是一个好的设计吗?
编辑:通过吞咽异常,我的意思是不将它们扔给调用者。不过,我确实记录了异常,并将它们转换为 false/true 返回值。
最佳答案
我喜欢文章presented here中描述的方法,有相关讨论here ...
这个想法是考虑异常类型,并以不同的方式处理它们:
One type of exception is a contingency, which means that a process was executed that cannot succeed because of a known problem (the example he uses is that of a checking account, where the account has insufficient funds, or a check has a stop payment issued.) These problems should be handled by way of a distinct mechanism, and the code should expect to manage them.
The other type of exception is a fault, such as the IOException. A fault is typically not something that is or should be expected, and therefore handling faults should probably not be part of a normal process.
关于java - 如何处理执行许多不同背景更改的外观方法的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/833264/