Understanding Exceptions in C#

Exception Handling

Exception Handling is a feature that is used to avoid or control the runtime errors generated by the program.

There are the following 3 types of errors.

  1. Syntax Errors

    Syntax errors are also called compilation errors. These errors will occur when you compile the program and if there is any error in the syntax it will show the error, so if you have not written the program properly.

    Example: Missing Semicolon at the end of a line.

    a) You do something wrong and it shows you an error called a compilation error.

    b) If you know the subject properly the moment that error occurs you will know what is the mistake and you will rectify it.

  2. Logical Errors

    These are the ones where there is no error in the program. Syntactically everything is written properly but that the error is there in the logic, that is very difficult to identify because when you compile it will not show the error, when you run the program it will not show you the error but still error is there.

    Where is the error?

    Output

    The result will not be correct/accurate. There are logical errors that are very difficult to trace.

    Example:

    I want to do 5+3, instead of 5+3 by mistake I have written 5-3.

    Will it show an error?

    No

    Example:

    If you say 5+3 it will give the answer 8.
    If you say 5-3 it will give the answer 2.

    So it's up to you to recognize whether the answer is correct or not you should cross-check then you know whether the answer is correct/not.

  3. Runtime Errors

    Syntactically it is written properly but when you run the program you will get an error, why you will get the error at runtime when you have written it properly, these errors occur when you try to do some impossible tasks with the computer, that is not possible for your system to do.

Example

5/0--> infinity

  1. Exception handling doesn't provide a solution to the error.
  2. Whenever a runtime error occurs it avoids the runtime error on the screen and it provides a systematic end to the program.
  3. Exceptional handling should not be used for every runtime error that occurs in the program.

Note: It should be used as a LastChoice when it is not possible for a programmer to avoid the error by writing any logic.

Exception Handling consists of 4 Blocks:

  1. try
  2. catch
  3. finally
  4. throw

Try block:

  1. A Try block detects runtime errors.
  2. Whenever a runtime error occurs within the try block, it detects the error and sends the error information to the catch block.
  3. A Catch block receives the error information and provides a systematic end to the program.

Note: If any error occurs outside the try block then a runtime error appears on the screen.

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.    try  
  4.    {  
  5.       int x, y, z;  
  6.       x = 5;  
  7.       y = 0;  
  8.       z = x / y;  
  9.       MessageBox.Show("result is" + z.ToString());  
  10.    }  
  11.    catch (Exception ex)  
  12.    {  
  13.       MessageBox.Show("cannot divide by zero");  
  14.       MessageBox.Show(ex.Message);  
  15.       MessageBox.Show(ex.StackTrace);  
  16.       MessageBox.Show(ex.ToString());  
  17.    }  
  18. }  

Message

Message is a property that returns the error message generated by a program.

StackTrace:

It is a property that returns the details of error.

ToString():

It is a function that returns the complete information of the error.

If there is a possibility of various types of errors then multiple catch blocks are used, if different customized messages are required.

Note: A Catch block will work only when there is a runtime error.

Finally

The following describes a Finally block:

  1. It is executed irrespective behavior of the program.
  2. If there is runtime error in the program, the Finally block is executed after the catch block.
  3. If there is no runtime error in the program, the Finally block is executed after the try block.
This is generally used for completing/finishing, if anything is pending in your application that has not been completed in the previous block then you can finish it in the finally block.

For example: Normally used with database connections and filehandling connections to close the connections that could not be close in earlier part.
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.    try  
  4.    {  
  5.       int x,y,z;  
  6.       x=5;  
  7.       y=0;  
  8.       z=x/y;  
  9.       MessageBox.Show("result is"+z.ToString());  
  10.    }  
  11.    catch(DivideByZeroException ex)  
  12.    {  
  13.       MessageBox.Show("cannot divide by zero");  
  14.    }  
  15.    catch(NullReferenceException ex)  
  16.    {  
  17.       MessageBox.Show("cannot process null");  
  18.    }  
  19.    catch(Exception ex)  
  20.    {  
  21.       MessageBox.Show("unable to execute");  
  22.    }  
  23.    finally  
  24.    {  
  25.       MessageBox.Show("finally block executed");  
  26.    }  
  27. }   
Throw

A throw is used to generated the errors manually or programmatically rather than waiting from the program to generate the errors.
  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.    try  
  4.    {  
  5.       int x, y, z;  
  6.       x = 5;  
  7.       y = 0;  
  8.       if (y == 0)  
  9.       {  
  10.          DivideByZeroException d = new DivideByZeroException("cannot divide");  
  11.          throw d;  
  12.       }  
  13.       z = x / y;  
  14.       MessageBox.Show("result is" + z.ToString());  
  15.    }  
  16.    catch (DivideByZeroException ex)  
  17.    {  
  18.       MessageBox.Show(ex.Message);  
  19.    }  
  20. }  


Recommended Free Ebook
Similar Articles