Exception Handling

Explain exception handling in c sharp.

Exception handling in C#, supported by the try catch and finaly block is a mechanism to detect and handle run-time errors in code. The .NET framework provides built-in classes for common exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) does not provide a mechanism to handle these anomalies, the .NET runtime environment provide a default mechanism, which terminates the program execution.

try..catch..finally

C# provides three keywords try, catch and finally to implement exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for any cleanup work that needs to be done.

If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block. 

But in C#, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks.

If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto.

In C#, exceptions are nothing but objects of the type Exception. The Exception is the ultimate base class for any exceptions in C#. The C# itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion to ArgumentException etc.

Uncaught Exceptions

 The following program will compile but will show an error during execution. The division by zero is a runtime anomaly and program terminates with an error message. Any uncaught exceptions in the current context propagate to a higher context and looks for an appropriate catch block to handle it. If it can't find any suitable catch blocks, the default mechanism of the .NET runtime will terminate the execution of the entire program.

  1. //C#: Exception Handling   
  2. //Author: rajeshvs@msn.com  
  3. using System;  
  4. class MyClient  
  5. {  
  6.     public static void Main()  
  7.     {  
  8.         int x = 0;  
  9.         int div = 100/x;  
  10.         Console.WriteLine(div);  
  11.      }  
  12. }

The modified form of the above program with exception handling mechanism is as follows. Here we are using the object of the standard exception class DivideByZeroException to handle the exception caused by division by zero.

  1. //C#: Exception Handling
  2. using System;
  3. class MyClient
  4. {
  5.     public static void Main()
  6.     {
  7.         int x = 0;
  8.         int div = 0;
  9.         try
  10.         {
  11.             div = 100 / x;
  12.             Console.WriteLine("This linein not executed");
  13.         }
  14.         catch (DivideByZeroException)
  15.         {
  16.             Console.WriteLine("Exception occured");
  17.         }
  18.         Console.WriteLine($"Result is {div}");
  19.     }
  20. } 

In the above case, the program do not terminate unexpectedly. Instead, the program control passes from the point where exception occurred inside the try block to the catch blocks. If it finds any suitable catch block, executes the statements inside that catch and continues with the normal execution of the program statements.

 If a finally block is present, the code inside the finally block will get also be executed. 

  1. //C#: Exception Handling
  2. using System;
  3. class MyClient  
  4. {
  5.     public static void Main()
  6.     {
  7.         int x = 0;
  8.         int div = 0;
  9.         try
  10.         {
  11.             div = 100/x;
  12.             Console.WriteLine("Not executed line");
  13.         }
  14.         catch(DivideByZeroException)
  15.         {
  16.             Console.WriteLine("Exception occured");
  17.         }
  18.         finally
  19.         {
  20.             Console.WriteLine("Finally Block");
  21.         }
  22.         Console.WriteLine($"Result is {div}");
  23.     }
  24. }

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception