//Program to handle
an Exception using try-catch Implementation with generic catch block
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAExceptions
{
class ClsException2
{
static
void Main()
{
int
a, b, c;
Console.WriteLine("Enter Any Two Numbers:-");
try
{
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a / b;
Console.WriteLine("Quotient value is:-" + c);
}
catch
{
Console.WriteLine("Error Occured");
}
finally
{
Console.WriteLine("Code Executed");
}
Console.ReadLine();
}
}
}
|
Result:-
- In the above example there is no exception class used in catch block, so it is known as generic catch block
- But in the above example any kind of exception may occur same message will be displayed to the user cannot understand why error has occurred, to overcome this specific catch blocks are used
- Using specific catch blocks it is possible to know more information about the exceptions
Some important Exception Classes
- ArgumentException
- ArgumentNullException
- ArgumentOutOfRangeException
- ArithemeticException
- ArrayTypeMismatchException
- BadImageFormatException
- DivideByZeroException
- DllNotFoundException
- EntryPointNotFoundException
- Exception( Super class of all other exception classes)
- FormatException
- IndexOutOfRangeException
- InsufficientMemoryException
- InvalidCastException
- MemberAccessException
- NullReferenceException
- UnauthorizedAccessException
Properties with Exception
Classes
Message: This property
will store about the reason, why exception has occurred
Source: This stores
name of the application from which exception has been raised
HelpLink: This is used
to provide link to any file/URL to give help information to the user, when
exception is raised
No comments:
Post a Comment