This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

EXCEPTION HANDLING IN C#
(0 votes, average: 0 out of 5)
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.

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options