A nice article to get used to Exception.

I found a nice article to get used to Exception from here C# Station: C# Tutorial Lesson 15 – Introduction to Exception Handling. Below is my favourite parts.


Exceptions are unforeseen errors that happen in your programs. Most of the time, you can, and should, detect and handle program errors in your code. For example, validating user input, checking for null objects, and verifying the values returned from methods are what you expect, are all examples of good standard error handling that you should be doing all the time.

However, there are times when you don’t know if an error will occur. For example, you can’t predict when you’ll receive a file I/O error, run out of system memory, or encounter a database error. These things are generally unlikely, but they could still happen and you want to be able to deal with them when they do occur. This is where exception handling comes in.

When exceptions occur, they are said to be “thrown”. What is actually thrown is an object that is derived from the System.Exception class. In the next section, I’ll be explaining how thrown exceptions are handled withtry/catch blocks.

The System.Exception class provides several methods and properties for obtaining information on what went wrong. For example, the Message property provides summary information about what the error was, theStacktrace property provides information from the stack for where the problem occurred, and the ToString()method is overridden to reveal a verbose description of the entire exception.

Identifying the exceptions you’ll need to handle depends on the routine you’re writing. For example, if the routine opened a file with the System.IO.File.OpenRead() method, it could throw any of the following exceptions:

  • SecurityException
  • ArgumentException
  • ArgumentNullException
  • PathTooLongException
  • DirectoryNotFoundException
  • UnauthorizedAccessException
  • FileNotFoundException
  • NotSupportedException

It’s easy to find out what exceptions a method can raise by looking in the .NET Frameworks SDK Documentation. Just go to the Reference/Class Library section and look in the Namespace/Class/Method documentation for the methods you use. The exception in the list above were found by looking at the OpenRead() method definition of the File class in the System.IO namespace. Each exception identified has a hyperlink to its class definition that you can use to find out what that exception is about. Once you’ve figured out what exceptions can be generated in your code, you need to put the mechanisms in place to handle the exceptions, should they occur.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: