Showing posts with label what is exception handling in c#. Show all posts
Showing posts with label what is exception handling in c#. Show all posts

Saturday, August 13, 2016

Exception handling in C#



An exception is the disruption that rises during the execution of program such as divide by zero. In C# provide the following keywords for exception handling: try, catch, finally and throw.

try – try block identifies the statement that throw and exception.

catch – the catch block handle the exception it any exists.

finally – the finally block is used to execute a set of statement whether the exception is thrown or not.

throw – The throw keyword is used to throw an exception programmatically.

The example of the exception handing is given below: - 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication20
{
    class program
    {
        public static void Main()
        {
            int x = 0;
            int y = 0;
            try
            {
                y = 100 / x;
                Console.WriteLine("Line is not executed");
            }
            catch (DivideByZeroException de)
            {
                Console.WriteLine("Exception rises");
            }
            finally
            {
                Console.WriteLine("Finally Block");
            }
            Console.WriteLine("Result is {0}", y);
            Console.ReadKey();
        }
       
    }
}
Output -  


                                                                Author - Sachin Pathak