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 the 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 it 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 them.

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 an 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 entering the wrong data into 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 logical statements. In real-time environments, the first and foremost importance should be given for 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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HandleExceptionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.Write("Enter any two numbers:");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            if (b == 0)
            {
                Console.WriteLine("Second Number cannot be Zero");
            }
            else
            {
                c = a / b;
                Console.WriteLine("Result is :" + c);
            }
            Console.Read();
        }
    }
}

Output

Output

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

Try Catch Implementation

  • A try block can be followed by any number of catch blocks, where writing the final block is optional.
  • If a catch block is used without an exception class, it is known as a Generic Catch block.
  • If a catch block is used with an exception class, it is known as a 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 the try block, if there is an exception that occurs in any statement of the try block, then the lines of the exception statement given below are ignored and control jumps to catch the block. Subsequently, the 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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HandleExceptionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.Write("Enter any two numbers:");
            try
            {
                a = Convert.ToInt32(Console.ReadLine());
                b = Convert.ToInt32(Console.ReadLine());
                c = a / b;
                Console.WriteLine("result is :" + c);
            }
            catch
            {
                Console.WriteLine("error Occured");
            }
            finally
            {
                Console.WriteLine("Code executed");
            }
            Console.Read();
        }
    }
}

Output

CMD

Properties of Exception Class

  1. Message
  2. Source
  3. Helplink

A list of exceptions that occur in real-time scenarios is 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 that 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.


Similar Articles