Example to handle an Exception using try-catch Implementation with specific catch block


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Program to handle an Exception using try-catch Implementation with specific catch block
namespace CAExceptions
{
    class Exception3
    {
        static void Main()
        {
            int a, b, c;
            Console.WriteLine("Enter Any Two Numbers");
            try
            {
                a = Convert.ToInt32(Console.ReadLine());
                b = Convert.ToInt32(Console.ReadLine());
                c = a / b;
                Console.WriteLine("Quotient Value is " + c);
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }
            Console.ReadLine();
        }
    }
}



Output :-

  • In the above example, super class exception is used to handle the exceptions, But, if we use  super class of any relevant class is available, it will kill the execution performance of the program

  • So any time don’t use super class of any exception class to handle any exception when there is relevant class is available

No comments:

Post a Comment