Exception Handling in C#

An exception usually signals an error. Thought it doesn’t always indicate an eror, it can also signal some particularly unusual even in your program that deserves special attention.

Exception & Errors Handling

No mater how good our program is, it always have to be able to handle possible errors. Most applications today contain some form of error handling. Unfortunately, the level and quality of error handling varies greatly. Some applications provide so much error handling that the application constantly raises alarms, even if the user provides the correct input. Other applications provide error handling that only a developer could love?the messages are replete with jargon and the data is useful only if you designed the application. Still other applications provide one-size-fits-all error handling that simply tells the user an error occurred without saying what error it was or how to fix it.

Exceptions handling in C# .NET

C# .NET creates an object (or throw) when a particular error condition occurs. This object has data members that store information about the nature of the error. Although .NET provides with many predefined exception classes yet we can create our own exception classes.

The generic exception class in C# .NET is System.Exception which is derived from System.Object. There are two important classes derived from System.Exception e.g. System.SystemException and System.ApplicationException.

Example of Excaptions handling in C#

The execution flow enters into a try block. If the error occurs the control automatically goes to the catch block and then the finally block is executed.

finally block can be omited. We will discuss finally keyword in the enxt section

We can use as many try blocks as we need.

We can also catch multiple errors from our one try block. For example an overflow and an array index out of bounds. We assue that our code have two boolean variables, OverFlow, OutOfBounds, which indicates that exception is occured. So our example would look like

Using the Finally Keyword

The finally keyword is an addition to the try…catch statement. You can use it in addition to, or in place of, the catch keyword. Any code that appears as part of the finally portion of the statement will run even after an error occurs. This keyword comes in handy when you want to attempt to reduce the damage of an error. For example, you might need to close a file to ensure a graceful failure of your application. Here’s an example of the finally keyword in use.

As you can see, we’re using MyBadCall1() again to generate an error. In this case, the catch portion of the try…catch statement will generate a simple essage telling you of the error. The message in the finally portion of the try…catch statement will also appear, despite the return statement in the catch portion. The finally code will always run. However, because of the return statement, the message box outside the try…catch statement will never appear.

Leave a Reply