Exception handling in C#

C# Exception Handling: Here, we will find out about the special case taking care of in C# with examples, here we will likewise observe the different basic exemptions and dealing with them utilizing attempt get a block.

What a special case is?

A special case is a runtime mistake; that implies a strange circumstance which is made at run time and the program doesn’t execute effectively. Because of the exemptions, our program gets crash.

There are following regular exemptions are happened during the program:

  • Separation by Zero
  • Document not found
  • Cluster Index out of binding
  • Invalid reference special case
  • Invalid cast special case
  • Math special case
  • Flood special case
  • Out of memory special case and so on.

Here is an example/program, that will create a special case

using System;

class Program
{
    static void Main()
    {
        int number1 = 0;
        int number2 = 0;
        int result  = 0;

        Console.WriteLine("Enter Number : ");
        number1 = int.Parse(Console.ReadLine());

        result = number1 / number2;

        Console.WriteLine("Result : " + result);
    }
}

At the point when we aggregate the above program, it doesn’t create any blunder. Arrangement result will be this way,

------ Build started: Project: ConsoleApplication1, Configuration: Debug x86 ------
ConsoleApplication1 ->C:\Users\Arvind\Documents\Visual Studio 2010\Projects\JTech\
ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

Furthermore, when we execute the program then it creates an exemption like this, (in the program, we are attempting to partition a number by 0).

Exception handling in C#

The above program produces “Gap by Zero special case” and the program gets smashed. Since variable number1 is partitioned by number2 and output put away in the outcome variable. Where, the estimation of number2 is 0, in this manner, “separate by zero special cases” happened.

To deal with such sort of special cases, we use exemption taking care of in C#.

attempt get lastly blocks

Special case taking care of in C# can be taken care of utilizing three blocks:

  • attempt
  • get
  • at last

Syntax of special case taking care of blocks,

    try
    {
        // code that can occur an exception
    }
    catch(Exception Type)
    {
        // code to handle the exception 
        // code to display the error message
    }
    finally
    {
        // code that should be executed 
        // whether exception is occurred or not
    }

1) attempt block

In this block, we compose the code that can create a special case or runtime mistake. For example, in the past program, there was an exemption (separate by zero).

2) get the block

This block is executed when the code tosses an exemption that is written in an attempted block, get block gets the special case and we can compose the code to deal with that exemption or any message to show the exemption according to client inclination.

Here, we utilized the exemption classes, there are the accompanying special case classes utilized in C# to get the exemptions.

  • Special case
  • DivideByZeroException
  • NullReferenceException
  • OutOfMemoryException
  • InvalidCastException
  • ArrayTypeMismatchException
  • IndexOutOfRangeException
  • AirthmeticException
  • OverFlowExecption

These classes are accessible in System namespace. In the above classes, the Exception class is a superclass and others are sub-classes, The Exception class can get any sort of special case tossed by the attempted block.

3) at last block

at last, the block is executed in all cases for example regardless of whether the program is creating a special case or not, before leaving the program, compiler moves to the at long last block. This block can be utilized to compose the codes that are mandatory for the execution of a program like the end of the document, discharging the memory, and so forth.

Here is the code (with special case taking care of) to deal with separate by zero exemption.

using System;

class Program
{
    static void Main()
    {
        int number1 = 0;
        int number2 = 0;
        int result  = 0;

        try
        {
            Console.WriteLine("Enter Number : ");
            number1 = int.Parse(Console.ReadLine());

            result = number1 / number2;
            Console.WriteLine("Result : " + result);
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            Console.WriteLine("Program exited normally");
        }
    }
}

Output:

Exception handling in C#

Leave a Comment

error: Alert: Content is protected!!