C# Program for Exception Handling Using Try and Catch Handling

Following is the example in c sharp showing the use of try and catch method for exception handling….
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class AddNumbers
    {
        int result;
        AddNumbers()
        {
            result=0;
        }
        public void divide(int number1, int number2)
      
        {
            try
            {
                 result = number1 / number2;
               
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine(“Exception caught. {0}”, e);
            }
            finally
            {
                Console.WriteLine(“result is {0}”, result);
            }
        }
            public static void Main (string [] args)
            {
                AddNumbers Division = new AddNumbers();
                Division.divide(10,0);
                Console.ReadLine();
            }
    }
}
       

Add Comment