Spring Boot Application – Is it considered bad practice to throw EntityExists – or EntityNotFoundException from Service to Controller?

huangapple go评论62阅读模式
英文:

Spring Boot Application - Is it considered bad practice to throw EntityExists - or EntityNotFoundException from Service to Controller?

问题

我的理解是,如果数据库中不存在你正在寻找的对象,或者存在异常情况,就会引发这些异常?但是当我想要在MyServiceClass中处理不同的情况时,我可以使用它们吗?

抛出这些异常是否是不良实践,还是应该为例如如果数据库中不存在用户这种情况创建自己的异常?

在真实生产环境中是如何运作的?

提前感谢!

英文:

My understanding of these exceptions is if an object in the database that you are looking for doesn't exist or exists these gets thrown? But is it ok for myself to use when I want to handle different cases in MyServiceClass.

Is it bad practice to throw these exceptions or should I create my own Exceptions for let's say if a user dont exist in the database?

How does it work in a real production?

Thanks in advance!

答案1

得分: 1

你应该仅在与Java标准异常相比具有优势的情况下实现自定义异常。你的异常类名称应以Exception结尾。
但有时捕获标准异常并将其包装成自定义异常会更好。这样的异常的典型示例是应用程序或框架特定的业务异常。这允许你添加附加信息,并且你还可以为你的异常类实现特殊处理。

在这样做时,请确保将原始异常设置为原因。Exception类提供了特定的构造方法,接受Throwable作为参数。否则,你将丢失原始异常的堆栈跟踪和消息,这将使分析导致异常事件的过程变得困难。

public void wrapException(String input) throws MyBusinessException {
    try {
        // 进行一些操作
    } catch (NumberFormatException e) {
        throw new MyBusinessException("描述错误的消息。", e);
    }
}

如果自定义异常对客户端代码没有有用的信息,请尽量不要创建新的自定义异常。
而且,如果创建自定义异常,请确保:

  1. 对你指定的异常进行文档化
  2. 使用描述性消息抛出异常
  3. 首先捕获最具体的异常
  4. 不要记录日志再抛出异常
英文:

You should only implement a custom exception if it provides a benefit compared to Java's standard exceptions. The class name of your exception should end with Exception.
But it’s sometimes better to catch a standard exception and to wrap it into a custom one. A typical example for such an exception is an application or framework specific business exception. That allows you to add additional information and you can also implement a special handling for your exception class.

When you do that, make sure to set the original exception as the cause. The Exception class provides specific constructor methods that accept a Throwable as a parameter. Otherwise, you lose the stack trace and message of the original exception which will make it difficult to analyze the exceptional event that caused your exception.

public void wrapException(String input) throws MyBusinessException {
    try {
        // do something
    } catch (NumberFormatException e) {
       throw new MyBusinessException("A message that describes the error.", e);
    }
}

Try not to create new custom exceptions if they do not have useful information for client code.
And if you make a custom exception be sure to:

  1. Document the Exceptions You Specify
  2. Throw Exceptions With Descriptive Messages
  3. Catch the Most Specific Exception First
  4. Don’t Log and Throw

huangapple
  • 本文由 发表于 2020年10月23日 14:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64495277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定