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#
1 2 3 4 5 6 7 8 9 10 11 12 | try { //control for normal execution } catch { //eror handling mechanism } finally { //clean up the resources } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | try { //code for the program if (OverFlow == true) throw new OverflowException(); //more code for the program if(OutOfBounds == true) throw new IndexOutOfRangeException(); //If no error then continue the normal execution } catch(OverflowException ex) { //error handling code } catch(IndexOutOfRangeException ex) { //eror handling code } finally { //clean up the resources } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | private void btnThrow3_Click(object sender, System.EventArgs e) { try { // The call will fail because we've thrown an exception. MyBadCall1(12); } catch { // Display an error message. MessageBox.Show("Invalid Exception Error", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Try to return. return; } finally { // The example must run this code. MessageBox.Show("Must Run This Code", "Finally Code", MessageBoxButtons.OK, MessageBoxIcon.Information); } // The example will try to run this code, but won't // because of the exception. MessageBox.Show("This Code Won't Run", "Code Outside Try//Catch", MessageBoxButtons.OK, MessageBoxIcon.Information); } |
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.