java - 如何处理执行许多不同背景更改的外观方法的错误?

标签 java api oop exception

假设我有一个如下所示的服务:

public interface MyAccountService
{
    boolean create( String user );
}

create 方法执行了一些更改,即(为了讨论):

  1. 将消息添加到队列中
  2. 将一行添加到多个表中
  3. 创建 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/

相关文章:

java - 为什么在这里调用父类(super class)方法?

java - IBM Websphere 7,支持 JAX-WS Service 2.2,这可能吗?

api - 是否有可能获得带有 X 点赞帖子链接的 reddit 的 RSS 提要?

javascript - 谷歌饼图标签

java - 分析 Java/OOP 中类型的基数

powershell - Powershell:在AD中检查重复的电子邮件

delphi - 如果显式调用 Create,我可以使编译器失败吗?

java - 使用 Jackson 从子元素开始反序列化 XML

c++ - tesseract-ocr 如何包含 baseapi.h

java - while(rs1.next()) 不返回任何值