Handling Exceptions in C#


© Jose Aniceto
Articles in this Topic    Discussions in this Topic

Today's computing environment is complex. Layers upon layers of software now exist between the user and hardware devices. Take for example the different database connections available in Windows. In the beginning, programmers have APIs that access low level Windows system calls. Today, ADO is almost an abstract layer that it doesn't matter where your database server resides.

In such complicated environments, failures can occur at different levels. When this occurs, C# applications and programs have a mechanism to respond to such failures. According to Jesse Liberty, author of Programming C#, exception handling is not a substitute to bug-free software. I could not agree more.

Exception is used to ensure that applications respond accordingly to its environment. For example if a C# application couldn't read from disk because of a disk crash, through exceptions, the C# application can decide on how to continue. It may perhaps try accessing a backup disk or try and see if the contents of the file are in memory.

C# supports exception handling through the use of the following commands; throw, catch, try and finally. Since exceptions can occur at any time and on any level, .Net has encapsulated exceptions in the System.Exception namespace. If you're not familiar with namespaces, we briefly discussed it in a (link) previous article.

To ensure that your application can handle an exception, you must enclose your code within two commands - try and catch. The following is an example of how to use try and catch.

Sample Code 1

01
02
03
04
05
06
07
08
09
10
11
12
class TestApp {
  public static void Main( void ) {
   int i;

   try {
    i = i +1;
   }
   catch {
    Console.WriteLine("Exception caught.");
   }
  }
}

The idea behind the try and catch command is that if any anomalies happen within the try block, the catch block will be executed. Unfortunately the sample code above is not a good example because line 6 will not cause an exception.

It is possible to generate an exception in C#. The command to use to generate an exception is throw. The System namespace also has a function called Exception() which will be used to throw exceptions.

Normally in C++ programs, the try command is used to free up any unallocated memory space used by malloc(). In C#, memory management is handled automatically. The next example, Sample Code 2, shows how to use all three commands.

Sample Code 2

01
02
03
04
05
06
07
08
09
10
11
12
13
using System;
class TestApp {
  public static void Main( void ) {
   int i;

   try {
    i = i +1;

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo