Microsoft provides a bunch of standardized error codes for Windows (http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx). When applicable I find them useful to reference in my own applications, instead of creating my own. Is there something similar but specific to .NET?
2 Answers
.NET discourages the use of error codes. Instead, focus on using, or deriving from, the standard Exception classes.
Exceptions should be preferred to error codes in .NET development.
10 Comments
derGral
OK, that makes sense. However, part of the reason we're doing this is for our service department; so they can have a list of all error codes/messages that the system can generate/throw. Given that, is there a better way than using string resources? Is there a way to do reflection (of exceptions or otherwise) to have a list of errors?
Reed Copsey
Exceptions have the advantage of allowing the developer to provide context of the exception. You can include specific enum values for resources within custom exceptions, but also full text string explanations that help you narrow down where and what occurred.
derGral
Sure, I appreciate that. They just want a list of what can be encountered so they can review and ask questions before they have a customer on the phone. Maybe this should be posed as a separate question, but being able to get a list of potential error messages is still desirable.
Guillaume86
Looks like there is some opposite opinions on the subject: stackoverflow.com/a/2555307/172074. I'll go with exception handling personally
hackery
.NET can "discourage" all it likes, there are still places in Windows which require them. e.g. I'm writing a custom action script for an MSI, and if it fails it has to return a non-zero exit status to msiexec - that's a plain old number, there's no way to convey an exception context. If something goes awry, I need to synthesize ERROR_INSTALL_FAILURE (1603L from msi.h) - it's hard to believe that the recommendation would be to put an explicit numeric constant in my PS1 code.
|
I agree with Reed Copsey, error codes are "remainings" from the past error handling methods.
The new way of doing i is Exception Handling.