处理已知错误和错误信息的方法 [英] Handling Known Errors and Error Messages in a Method

查看:180
本文介绍了处理已知错误和错误信息的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好的方法来处理发生在一个方法已知的错误?

What are some good ways to handle known errors that occur in a method?

让我们用户注册方法为例。当用户签署了一个方法注册(用户用户)被调用。有迹象表明可能发生了一些已知的错误。

Let's take a user registration method as an example. When a user is signing up, a method SignUp( User user ) is called. There are a few known errors that might happen.

  • 电子邮件已经被注册
  • 在用户名已经被注册

您可能会引发特定的异常:

You could throw specific exceptions:

public void SignUp( User user )
{
    // Email already exists
    throw new EmailExistsException();
}

现在具体的例外可能被捕获。

Now specific exceptions could be caught.

这是在我看来不好的,因为异常被用于流量控制。

This is bad in my opinion, because exceptions are being used for flow control.

您可以返回一个布尔值,说明是否成功并传入将被置一个错误信息,如果发生错误:

You could return a boolean stating if it succeeded and pass in an error message that would get set if an error occurs:

public bool SignUp( User user, out/ref string errorMessage )
{
    // Email already exists
    errorMessage = "Email already exists.";
    return false;
}

我不喜欢这样的几个原因。

I don't like this for a few reasons.

  • 系统值必须返回。如果什么方法需要返回一个值?
  • 在错误消息传递在每一个时间。
  • 方法的消费者应该是一个确定该消息是什么。

让我们只说任何东西,其中实际消息中的方法设置是坏的。

Let's just say anything where the actual message set in the method is bad.

您可以使用错误codeS:

You could use error codes:

public enum Errors
{
    Successful = 0,
    EmailExists,
    UsernameExists,
    Etc
}

public Errors SignUp( User user )
{
    // Email already exists
    return Errors.EmailExists;
}

// or

public void SignUp( User user, out/ref Errors error )
{
    // Email already exists
    error = Errors.EmailExists;
}

这里的最后一个是我最喜欢的,但我还是不喜欢它了一大堆。我不喜欢传递一个错误code中的想法。我不喜欢要么返回一个code,对这个问题的想法。

The very last one here is the one I like best, but I still don't like it a whole lot. I don't like the idea of passing an error code in. I don't like the idea of returning an code either, for that matter.

我喜欢使用自定义异常,因为它似乎有点清洁的想法,但我不喜欢使用异常流量控制的思想。也许在特定的情况下,像这样的例子,电子邮件系统中已有的应该是一个例外是,和它的确定。

I like the idea of using custom exceptions because it seems a little cleaner, but I don't like the idea of using exceptions for flow control. Maybe in specific cases like this example, an email already being in the system SHOULD be an exception, and it's ok.

有什么其他人在这种情况下做了什么?

What have other people done in this situation?

推荐答案

在这种情况下,我会创建一个名为一个用户自定义异常 NewUserRegistrationException 有一个特殊的属性(名为原因),将包含的的原因的发生故障的。

In this case I would create an user defined exception called NewUserRegistrationException with a special property (named Reason) that would contain the reason of the failing.

使用你的榜样,枚举

public enum RegistrationErrorType
{
    Successful = 0,
    EmailAlreadyExists,
    UsernameAlreadyExists,
    Etc
}

为pretty的理解。

is pretty understandable.

那么,谁愿意通过调用你的方法注册一个新用户也可以用的ToString()异常弹出的一般性错误,或(在已经读文档开关原因财产和做出相应的反应(专注于电子邮件字段,颜色有红密码等)。

Who then wants to register a new user by calling your method can just .ToString() the exception to popup the generic error, or (after having read the documentation) switch the Reason property and react accordingly (focus on the email field, colour with red the password, etc).

例如code:

public class NewUserRegistrationException : Exception
{
    public RegistrationErrorType Reason { get; private set; }
    public NewUserRegistrationException(RegistrationErrorType reason)
        : base()
    {
        Reason = reason;  
    }
    public NewUserRegistrationException(RegistrationErrorType reason, string message)
        : base(message)
    {
        Reason = reason;  //might as well create a custom message?
    }
    public NewUserRegistrationException(RegistrationErrorType reason, string message, Exception inner)
        : base(message, inner)
    {
        Reason = reason; //might as well create a custom message?
    }
}

这篇关于处理已知错误和错误信息的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆