Exception Handling in C#:-
It is used to handle unexpected run time errors of the program to protect the program from run time interruption.
C# provides a try and catch block to handle the exception, try block is used to write the program code, ana d catch block is used to handle the error message.
It is used to handle unexpected run time errors of the program to protect the program from run time interruption.
C# provides a try and catch block to handle the exception, try block is used to write the program code, ana d catch block is used to handle the 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();
}
}
Throw Keyword in C#:-
{
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();
}
}
}
It is used to call exceptions class manually.
int a=0;
if\(a<0):
throw new Exception();
Example of throw keyword:-
class ExceptionExample
{
static void Main()
{
int a=-100;
try
{
if (a < 0)
{
throw new DivideByZeroException();
}
Console.WriteLine(a);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Enter only positive data");
}
Console.ReadKey();
}
}
POST Answer of Questions and ASK to Doubt