Exception Handling In C#

Exception handling is the concept where there are lots of methodology defined to handle the exception or we can say error. It can be run time exception or compile time exception.

Basically exception or error is point to faults which can be occurred during the program execution or program development. If you don’t handle the exception, then it makes program faulty or give the wrong result.

There are many types of errors in C#. It can be syntax error or logical error.

Syntax Errors

When you make some typing mistake like you are writing the program and instead of writing, if you write IF then it will be a syntax error since C# is a case sensitive language.

  1. int myvalue = 10;  
  3. IF(myvalue > 10) //syntax error,   
  4. {  
  5.    //other code  
  6. }  
Runtime Errors

Runtime errors occur during execution of the program that is why we also called runtime exceptions. If you does not create logic in proper way, then it create runtime exception.
  1. int a = 10, b = 0;  
  3. var c = a / b; //DivideByZeroException  
How to handle error or exception

Using exception handling, we can handle exception in a proper way and show the accurate result as per user understanding. It is a mechanism to detect and handle run time errors. We can achieve this using Try-Catch-Finally blocks.

Try block

In the try block, you need to put those codes which can be created exception. The try block encloses the statements that supposed to be throwing an exception.
  1. int a = 10, b = 0;  
  2. try  
  3. {  
  4.    var c = a / b; //Statement which supposed to be create exception  
  5. }  
Catch block

If any exception comes on the time of program execution that Catch block handles any exception if one exists.
  1. catch(Exception ex)  
  2. {  
  3.    //Handle exception  
  4. }  
Finally block

If you think that some logic is necessary to execute in any condition, then it need to put in the finally block. It can be used for doing any clean-up process like releasing unused resources even if an exception is thrown.
  1. finally  
  2. {  
  3.    //Code must be executed.  
  4. }  
Full example of try-catch-finally:
  1. using System;  
  2. namespace ExceptionHandling  
  3. {  
  4.     class MyClass  
  5.     {  
  6.         int result;  
  7.         MyClass()  
  8.         {  
  9.             result = 0;  
  10.         }  
  11.         public void division(int num1, int num2)  
  12.         {  
  13.             try  
  14.             {  
  15.                 result = num1 / num2;  
  16.             }  
  17.             catch (DivideByZeroException e)  
  18.             {  
  19.                 Console.WriteLine("Exception caught: {0}", e);  
  20.             }  
  21.             finally  
  22.             {  
  23.                 Console.WriteLine("Result: {0}", result);  
  24.             }  
  25.         }  
  26.         static void Main(string[] args)  
  27.         {  
  28.             MyClass d = new MyClass();  
  29.             d.division(25, 0);  
  30.             Console.ReadKey();  
  31.         }  
  32.     }  
  33. }  
Key points about exceptions handling:


  1. Exceptions are types that all directly or indirectly derive from System.Exception class.
  2. A try block is used to throw multiple exceptions that can handle by using multiple catch blocks.
  3. The statements inside the finally block are always executed whether exception occurs or not in the program.
  4. The order of try-catch-finally is like this try-catch-catch….-finally.

So, finally you learned what is exception handling and how can we handle exceptions in C#.

Up Next
    Ebook Download
    View all
    View all