什么我们不能使用RuntimeException而不是创建自定义

什么我们不能使用RuntimeException而不是创建自定义

本文介绍了为什么我们不能使用RuntimeException而不是创建自定义异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我一直在寻找很多有关何时应该创建自定义异常的信息。我发现了以下示例:

1) I have been searching a lot on when exactly we should go creating custom exceptions. I found this example:

public class IncorrectFileExtensionException extends Exception {
    public IncorrectFileExtensionException () {

    }

    public IncorrectFileExtensionException (String message) {
        super (message);
    }

    public IncorrectFileExtensionException (Throwable cause) {
        super (cause);
    }

    public IncorrectFileExtensionException (String message, Throwable cause) {
        super (message, cause);
    }
}

提供上述自定义例外的真正价值是什么? 为什么不创建上面的自定义例外,为什么不能抛出一个新的RuntimeException(发生错误,e)?我在互联网上看到了很多创建类似的自定义例外的示例,但是我没有了解上述方法的真正好处是什么。

What's the real value in providing the above custom exception? Instead of creating the above custom exception, why can I not throw a new RuntimeException("error occurred", e)? I have seen many examples on the internet creating similar custom exceptions, but I don't understand what the real benefit in the above approach is.

如果我们创建类似 IncorrectFileExtensionException 之类的东西,那么我们的项目最终会带来许多自定义异常。

If we create something like IncorrectFileExtensionException, then our project ends up with many custom exceptions.

2)我也发现了这一点:

public class MyUncheckedBusinessException extends RuntimeException {

    private static final long serialVersionUID = -8460356990632230194L;

    private final ErrorCode code;

    public MyUncheckedBusinessException(ErrorCode code) {
        super();
        this.code = code;
    }

    public MyUncheckedBusinessException(String message, Throwable cause, ErrorCode code) {
        super(message, cause);
        this.code = code;
    }

    public MyUncheckedBusinessException(String message, ErrorCode code) {
        super(message);
        this.code = code;
    }

    public MyUncheckedBusinessException(Throwable cause, ErrorCode code) {
        super(cause);
        this.code = code;
    }

    public ErrorCode getCode() {
        return this.code;
    }
}

c> IncorrectFileExtensionException ,因为我们至少提供了一些错误代码。但是同样,如果所有自定义异常都以上述方式结束(我的意思是带有错误代码)怎么办?

This is to some extent better than IncorrectFileExtensionException, as we are providing some error code at least. But again, what if all custom exceptions ends up like the above (I mean with an error code)?

请不要拒绝投票,因为我实际上不是在问如何创建自定义例外,但是我的问题是:自定义例外的最佳实践是什么?我们到底应该什么时候去定制异常? IncorrectFileExtensionException 真的是一个好方法吗?

Kindly do not down vote, as I am really not asking how to create custom exceptions, but my question is: What's the best practice for custom exceptions? When exactly should we go for custom exceptions? Is IncorrectFileExtensionException really a good approach?

3)什么时候不应该创建自定义

推荐答案

public class MyBusinessRuntimeException extends RuntimeException {
    // copy all the constructors you need
}

这是一个好习惯,因为它有助于分组并区分错误类型。您可以声明我的API抛出 MyBusinessRuntimeException 类型,然后抽象出您无法控制的其他 RuntimeException 类型。它会告诉用户将要使用的异常,以及应该将其视为异常的异常。

It's a good practice because it helps to group and differentiate error types. You could claim "My API throws MyBusinessRuntimeException types" and abstract out other RuntimeException types you have no control over. It would tell the user what exception they are going to work with, and what exceptions they should consider as abnormalities.

IncorrectFileExtensionException 不应直接扩展 Exception 。它们处于完全不同的抽象级别。

IncorrectFileExtensionException shouldn't directly extend Exception. They are on completely different levels of abstraction.

考虑

 Exception
     FileException
         FileExtensionException
             IncorrectFileExtensionException
             UnsupportedFileExtensionException

这篇关于为什么我们不能使用RuntimeException而不是创建自定义异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:31