Sunday, September 3, 2017

5. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication14
{
    class Program
    {
        public static void Main(string[] args)
       {
            try
            {
           
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
             
                Console.WriteLine("In catch block: an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block: do any cleaning up here");
                Console.ReadKey();
            }

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplicationavi11
{
    class Lab11
    {
        public static void Main()
        {
            try
            {
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
                Console.WriteLine("In catch block:   an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block:   do any cleaning up here");
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment