找到你要的答案

Q:Is a custom exception suitable in this case?

Q:在这种情况下,自定义异常是否合适?

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? i.e. DomainUnreachableException because I'm not sure if anything listed in this table is suitable.

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. ConnectionFailureException appears to be suitable here as it's what's thrown if you try to use the object.
  • 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.
  • Domain does not exist or is unreachable. This is the one I'm not sure of.

我有一个代码,检查活动目录域是否存在。我把这个引用,因为域:

  • may not exist
  • may be unreachable/offline

我使用的代码如下:

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

我知道建议使用一个标准的例外。但是,在这种情况下,自定义异常是否更合适呢?即domainunreachableexception因为我不知道什么是适当的表。

编辑,只是一点点的背景下,我所展示的代码是一个方法,设置一个对象,准备进一步使用的一部分。如果某些条件不满足,对象基本上是无用的。我的条件如下:

  • SQL Server instance does not exist or is inaccessible. ConnectionFailureException appears to be suitable here as it's what's thrown if you try to use the object.
  • 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.
  • Domain does not exist or is unreachable. This is the one I'm not sure of.
answer1: 回答1:

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:

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.

你可以获得的对象,但它的状态是无效的在当前操作的上下文,根据它的描述InvalidOperationException异常类是合适的:

当方法调用对对象的当前状态无效时引发的异常。

如果您已经决定执行操作,则此异常适合抛出,这是一个方法运行的“决定”状态。

如果你的方法是做一个GUID = =空应该是描述一个外部资源或做出一个决定,它不检查,那么你不应该扔在所有,但返回一个描述对象而不是。

answer2: 回答2:

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.

Maybe you could use ActiveDirectoryOperationException?

当异常捕获代码知道此自定义异常时,创建自定义异常是有意义的,以便它可以执行特定于此异常类型的某些操作。如果在捕获自定义异常后没有采取特殊步骤,或将捕获基类异常,则不需要创建新类型。只是过一个有意义的信息的最佳拟合的异常类型已经在整箱的定义。

或许你可以用activedirectoryoperationexception?

answer3: 回答3:

You should throw standard exceptions when they properly describe the error that occurred. ArgumentException, ArgumentNullException, InvalidOperationException are very often a good choice.

In your case, you might consider throwing an 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.

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. 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).

Edit

As Kapol noticed ActiveDirectoryOperationException might be a good choice.

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. If the AD is hidden behind some abstraction, throwing it will cause a leaky abstraction.

当正确描述发生的错误时,应该抛出标准异常。引发ArgumentException,argumentnullexception,InvalidOperationException异常往往是一个很好的选择。

在你的情况,你可能会考虑将引发ArgumentException。如果只在不存在域时抛出异常,这将是一个很好的选择。然而,你也会把它当域存在,但不。因此也将引发ArgumentException如果域名是正确的抛出,给客户正确的信息。

在这种情况下抛出自定义异常似乎是一个很好的选择。我不知道任何标准的例外,将完全适合你的情况。

不抛出异常或applicationexception。客户端将无法正确处理这些异常(catch(异常)将捕获在该方法中抛出的任何异常,而不仅仅是抛出的异常)。

编辑

Kapol注意到activedirectoryoperationexception可能是一个不错的选择。

当抛出这样的特殊异常时,您必须考虑客户对您的方法的了解。如果他们知道这是一个广告操作,你可以抛出一个广告例外。如果广告隐藏在一些抽象的后面,抛出它会导致抽象的泄漏。

answer4: 回答4:

Make one custom exception similar to DriveNotFoundException A drive is unavailable or does not exist. name it DomainNotFoundEcxeption inherited from Exception.

做一个自定义的异常相似drivenotfoundexception驱动器不可用或不存在。它的名字domainnotfoundecxeption继承异常。

c#  exception