Tutorials
Desktop Programming
C Sharp
EXCEPTION HANDLING IN C#
Written by Christopher Sunday, 15 June 2008 15:50
INTRODUCTION
Every self-respected programmer should include exception handling techniques. Sometimes your application will generate an error. Regardless of who was responsible for this error, the programmer or the user, it is up to the first to include the necessary exception handling techniques to keep his/her program from crashing. The .Net environment provides useful techniques for avoiding disastrous errors such as try-catch statements and user-defined exceptions.
EXCEPTIONS
All exceptions in C# are objects thrown (or created) when a respective error occurs. This object contains information for the error which allows you to track down your problem. The generic exception class System.Exception is a general exception object which doesn’t provide any useful information at all and should be rarely used. Some common exception classes are the following ones:
·        System.SystemException: An exception of generic nature is used for exceptions thrown by the .Net.Â
·        System.ApplicationException: It is the base class for any other custom-made exception. If you want to implement custom error handling techniques you should derive the exception classes from this one.
·        StackOverflowException: When the area of memory allocated to the heap is full you get this exception. Should an exception of stack overflow occurs, the only thing left to do is to terminate the program.
·        EndOfStreamException: If you try to read past the end of a given stream, you will get this kind of exception.
Â
CATCHING EXCEPTIONS
When you have code subject to possible errors and you want to account for the possibilities of an exception being thrown, C# offers you the try-catch statement which divides your block code in three segments:
·        The try block. In the first stage you just write the code as you would normally do. If no errors are found, the flow of execution goes to the third block. If an exception is thrown execution goes to a catch statement in the next code block (catch).
·        The catch block. Here you implement methods for dealing with any possible errors of the try block. At the end of this block execution continues to the last code block (catch).
·        The finally block. This block is executed whether or not an exception was thrown. It is used for cleaning up resources that your program might have used and generic code that should always be executed.
The following snippet of code illustrates the basic usage of this error-handling technique:
       static void Main(string[] args)
       {
               try
               {
                   throw new OverflowException();
               }
               catch (OverflowException ex)
               {
                   Console.WriteLine("An overflow exception occured!!!");
               }
               finally
               {
                   //clean up
                   Console.WriteLine("Exiting...");
               }
               Console.ReadLine();
       }
In the try block we deliberately throw an overflow exception in order to see how it works. When the exception is thrown execution jumps straight to the catch statement of the appropriate exception and its code is executed. For each exception you need to have a different catch statement. Lastly, the code in the finally block is executed to clean up any resources, destroy objects, close network connections etc.
If you call a method inside such a try-catch block, this method is also subject to error-handling. Suppose the execution flow is at a point inside the called method and an error occurs. Execution stops immediately, the flow goes backwards – exiting the method and reaching the appropriate catch statement of the try-catch block. You can also have throw statements that are nested in several function calls inside the try block. Calling functions does not render the try block useless.

| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |