SEARCH C# PROGRAMMING CODES HERE

Exception Handling

Exception Handling in C#
An exception is an error that emerges during the execution of a program. The exception is a reaction to an uncommon condition that emerges while a program is running. Exceptions give an approach to exchange control starting with one piece of a program then onto the next. To perform exception handling To handle these situations in programming we can use 4 keywords try, catch, finally, and throw keyword to handle these situations. Apart from this, we can create user-defined exception handling function as per the requirements.
So let's go one step further and figure out how exception handling works.

1-try/catch keywords
The try block utilized to put the code inside the try block that may throw an exception wherein catch block utilized to take care of the exception.so let's see the example below which demonstrates try/catch block

Program

using System;

    namespace try_catch
    {

        class Program
        {

            static void Main(string[] args)
            {
              try
             {
            int x = 50;
            int y = 0;
            int z = x / y;
              }

        catch (Exception ex)
              {
            Console.WriteLine(ex);
            Console.ReadKey();
              }
             
            }
        }
    }
Output






As you can see in the above program it's showing System.DivideByZeroException: Attempted to divide by zero. So instead of displaying Exception Error, we can define our own catch block as per the requirement, So let's see the simple example which displays Error Message which says you can't Divide by Zero on the user screen code as follows.

Program
using System;

    namespace try_catch
    {

        class Program
        {

            static void Main(string[] args)
            {
              try
             {
            int x = 50;
            int y = 0;
            int z = x / y;
              }

        catch
              {
          Console.WriteLine("Error Message"+"\nYou can't Divide by Zero");
            Console.ReadKey();
              }
             
            }
        }
    }
     Output

















Similarly, you can perform the different task as well.

2-finally keyword
The finally block is utilized to execute some specific code which is to be executed whether an exception is thrown or not.so let's see the example below which demonstrates finally block.


Program

using System;

    namespace try_catch_finally
    {
        class Divide
        {
            int z;

            Divide()
            {
                z = 0;
            }
            public void division(int x, int y)
            {
                try
                {
                    z = x/y;
                }
                catch (DivideByZeroException ex)
                {
                    Console.WriteLine("Exception Occured {0}", ex);
                }
                finally
                {
                    Console.WriteLine("Result={0}", z);
                }
            }
            static void Main(string[] args)
            {

                Divide Div = new Divide();
                Div.division(50, 0);
                Console.ReadKey();
            }
      }
}
     
  Output
















 As you can see in the above program after completion of try and catch block execution, finally block release the required output (Result=0)that we have defined in the finally block.

3-throw keyword
The throw keyword is helpful to throw an exception manually during the execution of the program. see the example below which demonstrates finally keyword.

Program

using System;

    namespace try_catch_throw
    {
        class Divide
        {
         
            static void Main(string[] args)
            {
                try
                {
                    int x = 50;

                    int y = 0;

                    int z = x / y;
                }
                catch (Exception)
                {
                    throw;
                  
                }  
            }

      }
}
     
    Output


No comments:

Post a Comment