Exception Handling Mechanism In .NET

By reading the article, you will learn the points given below.

  • What are the types of errors occurring in .NET?
  • Methods to handle exception
  • Program to handle an exception, using Logical Implementation
  • Program to handle an exception, using Try Catch Implementation
  • Top 10 Interview questions on Exception Handling Mechanism

Let’s begin with an introduction on exception handling mechanisms,

When we write some code and execute in .NET, there is a high possibility of an error occurrence, as mentioned below.

  1. Syntactical Error
  2. Compilation Error
  3. Runtime Error

Syntactical Error

This error will occur by typing the wrong syntax like missing double quotes, terminators etc.

We as a programmer can identify these minor errors and can easily rectify.

Compilation Error

This error occurs when the program is compiled and the errors are like assigning wrong data to a variable, creating an object for interface etc.

This error can be identified and rectified before the execution of the program.

Run Time Error

This error will occur at the time of executing the program, errors are like entering the wrong data in to a variable trying to open a file for which there are no permissions, which are trying to connect with the database with wrong credentials etc.

What is Exception?

A run time error is known as an exception, as it will cause abnormal termination of the program execution. To avoid abnormal termination of the program execution, we need to handle the exceptions.

Methods to handle an exception:

There are two methods to handle the exception, which are given below.

  1. Logical Implementation
  2. Try & Catch implementation

Logical Implementation

In this method, we handle the exception by using the logical statements. In real time environments, the first and foremost importance should be given for the logical implementation only.

If it's not possible to handle any exception, using logical implementation, we use try catch implementation.

Let’s see a sample program on how to handle an exception, using Logical Implementation

Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace HandleExceptionDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int a, b, c;  
  14.             Console.Write("Enter any two numbers:");  
  15.             a = Convert.ToInt32(Console.ReadLine());  
  16.             b = Convert.ToInt32(Console.ReadLine());  
  17.             if (b == 0)  
  18.             {  
  19.   
  20.                 Console.WriteLine("Second Number cannot be Zero");  
  21.             }  
  22.             else  
  23.             {  
  24.                 c = a / b;  
  25.                 Console.WriteLine("Result is :" + c);  
  26.             }  
  27.             Console.Read();  
  28.         }  
  29.     }  
  30. }   

Output


When the user enters the second number as zero, an exception will be raised. This is handling, using logical implementation.

Try Catch Implementation

A try block can be followed by any number of catch blocks, where writing finally block is optional.

If catch block is used without an exception class, it is know as Generic Catch block.

If catch block is used with an exception class, it is know as Specific Catch block.

Try

This block contains all the statements in which there is a possibility of an exception occurrence.

Catch

This block contains all the statements to handle the exception, which is raised within the try block.

Process of execution

Execution starts from try block, if there is any exception which occurs in any statement of try block, then the lines of exception statement given below are ignored and control jumps to catch block. Subsequently, catch block is executed and then finally block. It's simple.

Let’s see a sample program to handle an exception, using Try-Catch implementation.

Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace HandleExceptionDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int a, b, c;  
  14.             Console.Write("Enter any two numbers:");  
  15.             try  
  16.             {  
  17.                 a = Convert.ToInt32(Console.ReadLine());  
  18.                 b = Convert.ToInt32(Console.ReadLine());  
  19.                 c = a / b;  
  20.                 Console.WriteLine("result is :" + c);  
  21.             }  
  22.             catch  
  23.             {  
  24.                 Console.WriteLine("error Occured");  
  25.             }  
  26.             finally {  
  27.                 Console.WriteLine("Code executed");  
  28.             }  
  29.             Console.Read();  
  30.              
  31.         }  
  32.     }  
  33. }   

Output


Properties of Exception Class

  1. Message
  2. Source
  3. Helplink

A list of exceptions that occur in real-time scenarios are given below.

  1. Argument Exception
  2. ArgumentNullException
  3. ArithmeticException
  4. IndexOutOfRange
  5. NullReferenceException
  6. InSufficientException
  7. T.C.,

Interview Questions on Exception Handling Mechanism

  • What are Runtime Errors?
  • What are the different types of errors which occur during program development and execution?
  • Explain about Logical Implementation
  • Explain about try catch implementation & nested try catch Implementation
  • What are the properties with the Exception Classes?
  • What is an Exception?
  • What is the process of an Exception?

I hope this article helps you to gain knowledge on an exception handling mechanism in .NET.

Thanks for reading and let me know your valuable comments, if any.