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.
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.
Type of exception block:-
1) try-catch block
try
{
CODE SECTION;
}
catch()
{
ERROR SECTION;
}
2) try---catch---finally block
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();
}
}

{
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();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class ExceptionDemo
{
int c;
void Division(int a, int b)
{
try
{
c = a / b;
Console.WriteLine(c);
}
catch (ArithmeticException ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
static void Main()
{
ExceptionDemo obj = new ExceptionDemo();
obj.Division(100, 0);
Console.ReadKey();
}
}
}

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