May 13, 2013

Exception Handling and What To Do

Exception handling is an interesting topic for me today because I dont know if I am using the right exceptions for the right error. There are a billion of exceptions, so how can we catch/throw these things to provide a better message to the user, or even better, how to correct these errors depending on what the exceptions are?

So, my journey started with, what is the best way to throw exceptions? I came to this blog article that brought up good points to take into account on exceptions:

 Taken from this site : http://blogs.msdn.com/b/kcwalina/archive/2006/07/05/657268.aspx Do's 1) Use System namespace exceptions 2) Do use custom exceptions that can be used to resolve the issue (i.e. a user-specific exception that is caught and handled automatically for the user) 3) Throw specific errors Dont's 1) Do not just catch and throw exceptions 

In #3 of the Do’s was the issue I was having recently. I would throw exceptions, but maybe not the ones outlined by Microsoft, or the documentation. After doing some Google searches, I found a couple of exception lists that helped with this issue:

 1)System.Exception 2)Exception Base Class 3)Exception Hierarchy 

Basically, here are the exceptions I found to be helpful:

# Exception Name What it’s used for? Namespace
1 ApplicationException This is an error that is non-fatal. Think of it like your generic exception to throw. System
2 SmtpException Couldn’t send some e-mail. System.Net.Mail
3 AggregateException If you have more than one error coming back, aggregate them in this exception. System
4 ValidationException Represents the exception that occurs during validation of a data field when the ValidationAttribute class is used. System.ComponentModel.DataAnnotations
5 HttpRequestException Exceptions for http related stuff. It is a base class used in HttpClient and HttpMessageHandler. System.Net.Http
6 ArgumentException The exception that is thrown when one of the arguments provided to a method is not valid. System
7 ArithmeticException The exception that is thrown for errors in an arithmetic, casting, or conversion operation. Side note, you could use System.InvalidCastException for casting, too. System
8 DataException Represents the exception that is thrown when errors are generated using ADO.NET components. System.Data
9 OperationAbortedException This exception is thrown when an ongoing operation is aborted by the user. System.Data
10 FormatException The exception that is thrown when the format of an argument does not meet the parameter specifications of the invoked method. System
11 IndexOutOfRangeException Index out of range, nuff said. System
12 InvalidOperationException If the object, needs to be initialized with the Start() method or something like that, and you don’t call it, use this one. System
13 IOException Issues with IO. System.IO
14 NotImplementedException Used to be temporary to let you know you didn’t finish functionality. System
15 NotSupportedException So, if you try to read a file that isn’t there anymore, you should give this error. System
16 NullReferenceException Used to throw an attempt to use a null object. System
17 SerializationException Used to throw for errors during serializing and deserializing. System.Runtime.Serialization
18 SecurityException A security error. Maybe log in stuff. System.Security
19 XmlSyntaxException For invalid XML, but why is it in Security namespace? I don’t know, you could just use System.Xml.XmlException, too. System.Security
20 CommunicationException Communication issue on server or client. System.ServiceModel
21 TimeoutException Timeout exception. There is also System.TimeoutException System.ServiceProcess
22 TypeInitializationException The exception that is thrown as a wrapper around the exception thrown by the class initializer. This class cannot be inherited. System
23 UnauthorizedAccessException The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error. System
24 FileNotFoundException File was not found System.IO
25 DirectoryNotFoundException Directory was not found. System.IO
26 ArgumentNullException For when your argument, that you pass in to a function, is null. System
27 ObjectDisposedException Object you are trying to use was disposed already, so you throw this error. System

Remember, this is guidelines, so if all else fails, write your own. If you have a general exception that you feel like is missing from the list, throw it in the comments and tell why to use it.