I usually do something like the example below when I need to return error messages from a function, if no errors occur I just return an empyty string. Is this best practice, or are there alternatve ways of returning error messages from functions?
当我需要从函数返回错误消息时,我通常会执行类似下面示例的操作,如果没有错误发生,我只返回一个empyty字符串。这是最佳实践,还是有替代方法从函数返回错误消息?
Function Test() as String
' Do something
If error occured Then
Return "Some error message"
Else
Return ""
End Functon
5 个解决方案
#1
Instead of returning an error message you should throw an exception that contains the error message.
您应该抛出包含错误消息的异常,而不是返回错误消息。
Here's a quick overview: http://www.vbdotnetheaven.com/UploadFile/rajeshvs/dotnetException04162005022135AM/dotnetException.aspx
以下是一个快速概述:http://www.vbdotnetheaven.com/UploadFile/rajeshvs/dotnetException04162005022135AM/dotnetException.aspx
#2
Exception Handling is the preferred method for dealing with errors. Error code return values can be obliviously ignored by developers using your functions. Exceptions force them to take notice. It's definitely worth learning about.
异常处理是处理错误的首选方法。使用您的函数的开发人员可能会忽略错误代码返回值。例外迫使他们注意到。这绝对值得学习。
When you wrote the code in your question, you probably assumed that it would be called like this:
当您在问题中编写代码时,您可能会认为它将被调用如下:
String message = Test();
// process the message for errors.
A lot of developers will just bypass processing the message, or even call your function like this:
许多开发人员只会绕过处理消息,甚至可以像这样调用你的函数:
Test();
// go about your business, happily ignoring the error message
If your code throws an exception instead, it cannot be ignored. A developer has to at least acknowledge that an exception is thrown by putting a try block around your function call. At that point they're forced to do something with it.
如果您的代码抛出异常,则不能忽略它。开发人员必须至少承认通过在函数调用周围放置一个try块来抛出异常。那时他们*做点什么。
#3
As Erik and Bill have said, exceptions are the normal way of propagating errors in .NET. However, there are situations where they're not appropriate - such as validating user input. At that point there are a couple of alternatives:
正如Erik和Bill所说,异常是在.NET中传播错误的常规方式。但是,有些情况下它们不合适 - 例如验证用户输入。那时有几种选择:
-
Use an error code (e.g. an enum) to indicate the type of mistake. For instance, you might have one code for "Password was too short" and another for "Password didn't contain any numbers" etc.
使用错误代码(例如枚举)来指示错误的类型。例如,您可能有一个代码为“密码太短”而另一个代码为“密码不包含任何数字”等。
-
Use an error message in the way that you've suggested in the question. I would personally use a null reference for the "it was okay" case or possibly make the method return a Boolean value (valid/invalid) and have an out parameter for the error message. Using a string is lousy for internationalisation, but is simpler in many ways (avoids extra lookups, easier to add a new kind of error etc) than the error code version. That may well be fine for an internal app which will never need to be internationalised.
按照您在问题中建议的方式使用错误消息。我个人会使用空引用来表示“它还可以”,或者可能使该方法返回一个布尔值(有效/无效)并为错误消息提供一个out参数。使用字符串对于国际化来说是糟糕的,但在许多方面(避免额外的查找,更容易添加新的错误等)比错误代码版本更简单。对于一个永远不需要国际化的内部应用程序来说,这可能会很好。
I stress that these are only options where exceptions don't make sense - otherwise, exceptions are the way to go.
我强调这些只是异常没有意义的选项 - 否则,异常是要走的路。
#4
A Request-Response pattern can help with handling errors and the many ways something might fail. For example, in a credit card authentication procedure you might have:
请求 - 响应模式可以帮助处理错误以及可能失败的许多方式。例如,在信用卡身份验证过程中,您可能具有:
class CreditCardAuthenticationRequest {
string CreditCardNumber;
string FullName;
...
}
class CreditCardAuthenticationResponse {
CreditProcessorStatusCode Status;
CreditProcessorWarnings[] Warnings;
CreditProcessorErrors[] Errors;
Exception Exception;
...
}
Now suddenly all your error handling and validation can be contained in a neat little package. The Patterns in Action sample application from DoFactory.com uses this extensively.
现在突然你所有的错误处理和验证都可以包含在一个整洁的小包中。来自DoFactory.com的Patterns in Action示例应用程序广泛使用它。
#5
I would agree that, in general, you would want to use exceptions for errors when the method would not otherwise return a value. If the method does return a value, however, you can and perhaps should use the return value in certain circumstances. For example, if you are attempting to retrieve an object from a keyed collection and the key doesn't exist, it's perfectly reasonable to return null as evidence of non-existence rather than throwing an exception. Your case doesn't seem to fit this scenario, however, and I would go with the exception.
我同意,一般情况下,当方法不会返回值时,您会希望使用异常来处理错误。但是,如果方法确实返回了值,则可以在某些情况下使用返回值。例如,如果您尝试从键控集合中检索对象并且该键不存在,则返回null作为不存在的证据而不是抛出异常是完全合理的。但是,你的情况似乎不适合这种情况,我会选择例外。
#1
Instead of returning an error message you should throw an exception that contains the error message.
您应该抛出包含错误消息的异常,而不是返回错误消息。
Here's a quick overview: http://www.vbdotnetheaven.com/UploadFile/rajeshvs/dotnetException04162005022135AM/dotnetException.aspx
以下是一个快速概述:http://www.vbdotnetheaven.com/UploadFile/rajeshvs/dotnetException04162005022135AM/dotnetException.aspx
#2
Exception Handling is the preferred method for dealing with errors. Error code return values can be obliviously ignored by developers using your functions. Exceptions force them to take notice. It's definitely worth learning about.
异常处理是处理错误的首选方法。使用您的函数的开发人员可能会忽略错误代码返回值。例外迫使他们注意到。这绝对值得学习。
When you wrote the code in your question, you probably assumed that it would be called like this:
当您在问题中编写代码时,您可能会认为它将被调用如下:
String message = Test();
// process the message for errors.
A lot of developers will just bypass processing the message, or even call your function like this:
许多开发人员只会绕过处理消息,甚至可以像这样调用你的函数:
Test();
// go about your business, happily ignoring the error message
If your code throws an exception instead, it cannot be ignored. A developer has to at least acknowledge that an exception is thrown by putting a try block around your function call. At that point they're forced to do something with it.
如果您的代码抛出异常,则不能忽略它。开发人员必须至少承认通过在函数调用周围放置一个try块来抛出异常。那时他们*做点什么。
#3
As Erik and Bill have said, exceptions are the normal way of propagating errors in .NET. However, there are situations where they're not appropriate - such as validating user input. At that point there are a couple of alternatives:
正如Erik和Bill所说,异常是在.NET中传播错误的常规方式。但是,有些情况下它们不合适 - 例如验证用户输入。那时有几种选择:
-
Use an error code (e.g. an enum) to indicate the type of mistake. For instance, you might have one code for "Password was too short" and another for "Password didn't contain any numbers" etc.
使用错误代码(例如枚举)来指示错误的类型。例如,您可能有一个代码为“密码太短”而另一个代码为“密码不包含任何数字”等。
-
Use an error message in the way that you've suggested in the question. I would personally use a null reference for the "it was okay" case or possibly make the method return a Boolean value (valid/invalid) and have an out parameter for the error message. Using a string is lousy for internationalisation, but is simpler in many ways (avoids extra lookups, easier to add a new kind of error etc) than the error code version. That may well be fine for an internal app which will never need to be internationalised.
按照您在问题中建议的方式使用错误消息。我个人会使用空引用来表示“它还可以”,或者可能使该方法返回一个布尔值(有效/无效)并为错误消息提供一个out参数。使用字符串对于国际化来说是糟糕的,但在许多方面(避免额外的查找,更容易添加新的错误等)比错误代码版本更简单。对于一个永远不需要国际化的内部应用程序来说,这可能会很好。
I stress that these are only options where exceptions don't make sense - otherwise, exceptions are the way to go.
我强调这些只是异常没有意义的选项 - 否则,异常是要走的路。
#4
A Request-Response pattern can help with handling errors and the many ways something might fail. For example, in a credit card authentication procedure you might have:
请求 - 响应模式可以帮助处理错误以及可能失败的许多方式。例如,在信用卡身份验证过程中,您可能具有:
class CreditCardAuthenticationRequest {
string CreditCardNumber;
string FullName;
...
}
class CreditCardAuthenticationResponse {
CreditProcessorStatusCode Status;
CreditProcessorWarnings[] Warnings;
CreditProcessorErrors[] Errors;
Exception Exception;
...
}
Now suddenly all your error handling and validation can be contained in a neat little package. The Patterns in Action sample application from DoFactory.com uses this extensively.
现在突然你所有的错误处理和验证都可以包含在一个整洁的小包中。来自DoFactory.com的Patterns in Action示例应用程序广泛使用它。
#5
I would agree that, in general, you would want to use exceptions for errors when the method would not otherwise return a value. If the method does return a value, however, you can and perhaps should use the return value in certain circumstances. For example, if you are attempting to retrieve an object from a keyed collection and the key doesn't exist, it's perfectly reasonable to return null as evidence of non-existence rather than throwing an exception. Your case doesn't seem to fit this scenario, however, and I would go with the exception.
我同意,一般情况下,当方法不会返回值时,您会希望使用异常来处理错误。但是,如果方法确实返回了值,则可以在某些情况下使用返回值。例如,如果您尝试从键控集合中检索对象并且该键不存在,则返回null作为不存在的证据而不是抛出异常是完全合理的。但是,你的情况似乎不适合这种情况,我会选择例外。