C#.Net Exception Handling

  • An exception can be considered as an error which is caused due to a run time error in a program.
  • When the compiler encounters with the error such as dividing by zero, it creates an exception object and informs us that an error has occurred.
  • If we want that the program remains continue executing the remaining code, we have to catch the exception object thrown by the error condition and display an appropriate message for taking corrective action. This process is known as the exception handling.
  • If we do not catch and handle the exception object properly the compiler will display an error message and will terminate the program.

In C#.net Exception can be divided into two parts:

exception-handling

We will discuss these Exceptions in details later, now we are going to discuss some important terms regarding exception handling:

  • try block:The code that may be throw exception which we want to handle is put in the try block. It is followed by one and more catch block.
  • catch block:The code to handle the thrown exception is put in the catch block just after the try block.
  • finally block:finally block is always executed whether the exception is thrown or not by the try block.
  • throw statement:Any method can throw the exception using throw statement when any unexpected event is occur.

Standard Exceptions

  • The exceptions which are pre-defined and provide us with the help of some classes called Exception classes by .NET Class Library are known as Standard Exceptions.
  • These Exception classes are defined in the System namespace.
  • Some of the exception classes present in .NET class library are given below in the table:
  • exception-handling
Handling Standard Exceptions using try-catch block
  • The piece of code to handle the exception is written in the try-catch block.
  • When a program starts executing, the program control enters the try block. If all the statements present in try block successfully executed, then the program control leaves the try-catch block.
  • If any statement fails to execute, the execution of remaining statements is stopped and the program control goes to the catch block for handling the exception.
Some facts about try-catch block
  • A try block must be followed by a catch block.
  • A try block can have multiple catch blocks.
  • The try block must lie inside a method.

Here is an example of handling exceptions using try-catch block:

Example
  • Create a form with following design
  • exception-handling
  • Here we have taken two textboxes to take the values from user.
  • When user enters the value in b as 0 then the message will display because here we handled the exception divide by 0.
  • Double click on “Divide” button and add the following code:
  • private void button1_Click(object sender, EventArgs e)
    {
    int a = Convert.ToInt32(textBox
  • Text);
    int b = Convert.ToInt32(textBox
  • Text);
    int res;
    try
    {
    res = a / b;
    MessageBox.Show("Division is : "+res,"Exception Handling");
    }
    catch (Exception)
    {
    MessageBox.Show("b can not be zero");
    }
    }

  • In this code, textbox1 value is converted into integer and assigned to the variable a of int type.
  • In this code, textbox2 value is converted into integer and assigned to the variable b of int type.
  • Here we have declared res variable to hold the division result.
  • Inside try block we wrote the statement res = a/b if any exception occurs then try block detects exception and transfer to the catch block.
  • And inside the catch block, handling code will be executed.
  • Now press F5 to run the application.
  • exception-handling
  • When we provide other value in b then
  • exception-handling