Below is the code which demonstrates how exception is handled in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exception_Demo
{
class Program
{
int x, y, z;
Program()
{
x = y = z = 0;
}
static void Main(string[] args)
{
Program p = new Exception_Demo.Program();
try
{
p.x = Int32.Parse(args[0]);
p.y = Int32.Parse(args[1]);
p.z = p.x / p.y;
}
catch(ArithmeticException ex)
{
Console.WriteLine("Error={0}" + ex.Message);
}
catch (IndexOutOfRangeException ex1)
{
Console.WriteLine("Error={0}" + ex1.Message);
}
catch (Exception ex2)
{
Console.WriteLine("Error={0}" + ex2.Message);
}
finally
{
Console.WriteLine("Now in finally...");
}
Console.WriteLine("Div={0}", p.z);
Console.ReadLine();
}
}
}
Comments
Post a Comment