简体   繁体   English

异常命名约定和 scope

[英]Exception naming conventions and scope

We have multiple .NET 6 projects with Nullable Reference Types enabled.我们有多个 .NET 6 项目启用了可空引用类型。 So far, we've been too lazy to assign or create exception types for specific errors.到目前为止,我们懒得为特定错误分配或创建异常类型。

The most commonly used Exception types currently in our code are ArgumentException and ArgumentNullException.目前我们代码中最常用的Exception类型是ArgumentException和ArgumentNullException。

The use cases for the latter are mostly:后者的用例主要是:
In full properties: (I think these are fine)在完整属性中:(我认为这些很好)

public string OfferId 
{ 
    get => _offerId ?? throw new ArgumentNullException($"{nameof(ItemDTO)}.{nameof(OfferId)}");
    set => _offerId = value;
}

In constructors: (these are also debatably fine)在构造函数中:(这些也有争议)

NativeProductId = rawProduct.NativeProductId ??
            throw new ArgumentNullException($"{nameof(rawProduct)}.{nameof(rawProduct.NativeProductId)}");

And then we use them for anything that remotely smells of null , using them if an HttpRequest returns empty content, if JsonConvert returns null, etc. (this is obviously bad)然后我们将它们用于任何远程闻到null的东西,如果 HttpRequest 返回空内容,如果 JsonConvert 返回 null 等,则使用它们,等等(这显然很糟糕)

Now when we want to refactor, it is difficult to understand how specific exceptions are supposed to be when handling different errors.现在我们要重构的时候,很难理解在处理不同的错误时,具体的异常应该是怎样的。

(I found surprisingly little information about this when googling). (我在谷歌搜索时发现了很少的关于此的信息)。

Ie a custom exception 'DataNotFoundException' seems incredibly broad, but can probably describe both the cases (empty content and deserialization).即自定义异常“DataNotFoundException”似乎非常广泛,但可能可以描述这两种情况(空内容和反序列化)。

On the other hand, 'ResposeContentNullException' and 'JsonDeserializationNullException' are very specific, but means that lots of them have to be created to cover all the cases.另一方面,“ResposeContentNullException”和“JsonDeserializationNullException”非常具体,但这意味着必须创建很多它们才能涵盖所有情况。

Are there any rules or unwritten conventions that you good people of stack-overflow follow when creating, naming and using exceptions? stack-overflow 的好人在创建、命名和使用异常时是否有任何规则或不成文的约定?

Exceptions can be seen as a "special kind of return value".异常可以看作是一种“特殊类型的返回值”。 They are part of your method contract.它们是您的方法合同的一部分。 If your methods are documented, that documenation should include a list of the exceptions that can be thrown and under which circumstances they are thrown (cf. this example from the base class library).如果您的方法已记录,该文档应包括可以抛出的异常列表以及在何种情况下抛出异常(参见基础 class 库中的示例)。 Thus, in my opinion, exceptions should be designed/named with the caller of your method in mind.因此,在我看来,在设计/命名异常时应考虑到方法的调用者

In other words, ResposeContentNullException and JsonDeserializationNullException are much too specific, because they contain information about implementation details of your method ---details which the caller of your method should not need to care about.换句话说, ResposeContentNullExceptionJsonDeserializationNullException过于具体,因为它们包含有关您的方法的实现细节的信息——您的方法的调用者不需要关心的细节。

Instead, check the documentation and find out under which circumstances the response content can be null:而是查看文档,找出什么情况下响应内容可以是null:

  • If the response content is null when a communication failure occurs, an IOException might be appropriate.如果发生通信失败时响应内容为null,则IOException可能是合适的。

  • If the response content can never be null (according to the documentation), the user encountered a bug in your code and/or the base class library.如果响应内容永远不会是 null(根据文档),则用户在您的代码和/或基本 class 库中遇到了错误。 In that case, an exception signaling that an assertion failed would be most appropriate.在这种情况下,发出断言失败信号的异常将是最合适的。 Unfortunately, there is none in the core libraries, so I personally (ab)use InvalidOperationException for that.不幸的是,核心库中没有,所以我个人(ab)为此使用InvalidOperationException A custom LogicErrorException , AssertionFailedException or ShouldNeverHappenException would be a cleaner solution.自定义LogicErrorExceptionAssertionFailedExceptionShouldNeverHappenException将是更简洁的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM