Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Exception Handling in C#:-


It is used to handle unexpected run time errors of the program to protect the program form run time interruption.

C# provides try and catch block to handle the exception, try block is used to provide program code and catch block is used to handle error message.

try
{
    program code

}
catch(Exception ex)
{
   error message;
}
finally
{
Default Exception Block means it will execute under try and catch both.
}

Example of Division Program to Handle Exception in C#:-

 class ExceptionDemo
    {
        static void Main()
        {
            int a, b, c;
            try
            {
                Console.WriteLine("Enter First Number");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Second Number");
                b = int.Parse(Console.ReadLine());
                c = a / b;
                Console.WriteLine("Result is {0}", c);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            Console.ReadKey();

        }
    }

Example of Exception handling using multiple catch block:-

  class ExceptionDemo
    {
        static void Main()
        {
            int a, b, c;
            try
            {
                Console.WriteLine("Enter First Number");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Second Number");
                b = int.Parse(Console.ReadLine());
                c = a / b;
                Console.WriteLine("Result is {0}", c);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Enter Numeric Value");
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Denominator Can Not Be Zero");
            }
            Console.ReadKey();

        }
    }



Example of Try-catch and default block in c#:-

class ExceptionDemo
    {
        static void Main()
        {
            int a, b, c;
            try
            {
                Console.WriteLine("Enter First Number");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Second Number");
                b = int.Parse(Console.ReadLine());
                c = a / b;
                Console.WriteLine("Result is {0}", c);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Enter Numeric Value");
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Denominator Can Not Be Zero");
            }
            finally
            {
                Console.WriteLine("Default Block");
            }
            Console.ReadKey();

        }
    }








Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post