C# exceptions

Started by
2 comments, last by SiCrane 16 years, 7 months ago
Hi, In C# and .NET programming are exceptions the recommended mechanism to handle errors? Does anyone ever return error codes in C#?
-----Quat
Advertisement
Yes, Exceptions should be used for errors. The MS Design Guidelines say:
Quote:Do not return error codes. Exceptions are the primary means of reporting errors in frameworks.


IMHO, this is a pretty good idea :-)
Constantly checking return codes isn't very convenient.
Exception handling is the way to go with .Net. Many of the the .Net functions do not return anything so you couldn't return code check them.

theTroll
The corollary to that statement is that you shouldn't use exception handling for things that aren't errors. Example: Parse() vs. TryParse(). Parse() throws an exception if the input string can't be turned into a object of the appropriate type. TryParse() returns true or false indicating if the conversion succeeded. The former is used when you know that the string should be of the right type and the latter if you're not sure.

This topic is closed to new replies.

Advertisement