简体   繁体   English

在这种情况下是否适合使用自定义例外?

[英]Is a custom exception suitable in this case?

I've got a piece of code which checks if an active directory domain 'exists'. 我有一段代码可以检查活动目录域是否“存在”。 I've put that in quotes because the domain: 我将其用引号引起来,因为该域:

  • may not exist 可能不存在
  • may be unreachable/offline 可能无法访问/离线

The code I'm using is as follows: 我使用的代码如下:

var directoryEntry = new DirectoryEntry($"LDAP://{domainName}");
if(directoryEntry.Guid == null)
{
    // What exception do I throw here?
}

I know it's recommended to use one of the standard exceptions wherever possible. 我知道,建议尽可能使用标准例外之一。 But would a custom exception be more appropriate in this case? 但是在这种情况下,自定义例外会更合适吗? ie DomainUnreachableException because I'm not sure if anything listed in this table is suitable. DomainUnreachableException因为我不确定表中列出的内容是否合适。

Edit, just for a bit of context, the code I've shown is part of a method which sets up an object ready for further use. 编辑(仅出于上下文考虑),我显示的代码是设置对象以备将来使用的方法的一部分。 If certain conditions aren't met, the object is basically unusable. 如果不满足某些条件,则该对象基本上是不可用的。 The conditions I have are as follows: 我的条件如下:

  • SQL Server instance does not exist or is inaccessible. SQL Server实例不存在或不可访问。 ConnectionFailureException appears to be suitable here as it's what's thrown if you try to use the object. ConnectionFailureException在这里似乎很合适,因为如果您尝试使用该对象,则会抛出该异常。
  • Database does not exist or is inaccessible. 数据库不存在或不可访问。 I've used NullReferenceException here since the indexer returns null if it cannot access or find the database. 我在这里使用了NullReferenceException ,因为如果索引器无法访问或找到数据库,则它返回null。
  • Domain does not exist or is unreachable. 域不存在或不可访问。 This is the one I'm not sure of. 这是我不确定的那个。

You can obtain the object, but it's state is not valid for in the context of current operation so the InvalidOperationException Class is appropriate according to it's description: 您可以获取该对象,但是在当前操作的上下文中该对象的状态无效,因此InvalidOperationException类根据其描述是合适的:

The exception that is thrown when a method call is invalid for the object's current state. 当方法调用对于对象的当前状态无效时引发的异常。

This exception is suitable to throw if you have already decided to carry out an operation, that is a 'decided' state for a method to run. 如果您已经决定执行某项操作(该状态是方法运行的“已决定”状态),则适合抛出此异常。

If your method which is doing a check for Guid == null is supposed to describe an external resource or it's absence to make a decision, then you should not throw at all, but return a description object instead. 如果您正在检查Guid == null方法应该描述一个外部资源,或者不需要做出决定,那么您根本不应该抛出,而是返回一个描述对象。

Creating a custom exception makes sense when the exception-catching code is aware of this custom exception, so that it can perform some actions specific to this exception type. 当异常捕获代码知道此自定义异常时,创建自定义异常是有意义的,以便它可以执行特定于此异常类型的某些操作。 If no special steps should be taken after catching your custom exception or a base class exception will be caught, then there is no need to create a new type. 如果在捕获您的自定义异常后不应该采取任何特殊步骤,否则将捕获基类异常,则无需创建新类型。 Just pass a meaningful message to the best-fitting exception type already defined in the FCL. 只需将有意义的消息传递给FCL中已定义的最合适的异常类型即可。

Maybe you could use ActiveDirectoryOperationException ? 也许您可以使用ActiveDirectoryOperationException

You should throw standard exceptions when they properly describe the error that occurred. 当它们正确描述发生的错误时,您应该抛出标准异常。 ArgumentException , ArgumentNullException , InvalidOperationException are very often a good choice. ArgumentExceptionArgumentNullExceptionInvalidOperationException通常是一个不错的选择。

In your case, you might consider throwing an ArgumentException . 在您的情况下,您可以考虑抛出ArgumentException It would be a good choice if the exception was thrown only when a domain doesn't exist. 如果仅当域不存在时才引发异常,这将是一个不错的选择。 However, you will also throw it when the domain exists, but is unreachable. 但是,当域存在但无法访问时,您也会将其抛出。 So the ArgumentException will also be thrown if the domain name is correct, giving the client incorrect information. 因此,如果域名正确,也会引发ArgumentException ,从而为客户端提供错误的信息。

It seems that throwing a custom exception is a good choice in this case. 在这种情况下,抛出自定义异常似乎是一个不错的选择。 I don't know any standard exception that will perfectly fit your case. 我不知道任何适合您情况的标准例外情况。

And never throw Exception or ApplicationException . 而且永远不要抛出ExceptionApplicationException Clients will not be able to handle these exceptions correctly ( catch(Exception) will catch any exception thrown in the method, not only the one you throw). 客户端将无法正确处理这些异常( catch(Exception)将捕获方法中引发的任何异常,而不仅是您引发的异常)。

Edit 编辑

As Kapol noticed ActiveDirectoryOperationException might be a good choice. Kapol注意到ActiveDirectoryOperationException可能是一个不错的选择。

When throwing specialized exceptions like this, you have to consider what the clients know about your method. 当抛出这样的特殊异常时,您必须考虑客户端对您的方法的了解。 If they know it's an AD operation, you can throw an AD exception. 如果他们知道这是一个AD操作,则可以引发AD异常。 If the AD is hidden behind some abstraction, throwing it will cause a leaky abstraction. 如果AD隐藏在某些抽象后面,则将其抛出会导致泄漏的抽象。

Make one custom exception similar to DriveNotFoundException A drive is unavailable or does not exist. 使一个类似于DriveNotFoundException A drive is unavailable or does not exist.自定义异常DriveNotFoundException A drive is unavailable or does not exist. name it DomainNotFoundEcxeption inherited from Exception. 将其命名为DomainExceptionFoundEcxeption继承自Exception。

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

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