You are on page 1of 4

ExceptionHandling:-

At the time of appn development different types of errors may occur


Errors are of 2 Types:-
1. Compiletime Error
2. Runtime Error
Compiletime Error:- are also called as syntax errors
The Errors that wioll occur at the time of compilation of the program are called as
Compiletime Errors
Q)Can we rectify compiletime Errors?
Yes we can rectify,because whenever compiletime error will occur then csc
compiler will display the error message to Developer ,so that developer will
read the error message and rectify
Q)what is Runtime Error?
The Error that will occur at the time of execution of the program is called as
Runtime
Q)what is an Exception?
Exception is a Runtime Error
Q)why Exception will occur?
Exeception will occur because of the invalid i/p given by the enduser or due to
some network issues or some server issues or invalid logic written by
developer
Q)what will happen when Exception will occur?
whenever an Exception will occur then abnormal termination of program
Q)Can we rectify Runtime Errors?
No we cannot rectify Runtime Errors but we can handle them
Q)How to handle Runtime Errors?
we can handle Runtime Errors in 3 ways:-
1. Logical Implementation
2. try-catch Implementation
3. Application Exception
using System;
class MyClass
{
static void Main(string[] args)
{
Console.WriteLine("Enter First no");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int y= int.Parse(Console.ReadLine());
int z = x / y;
Console.WriteLine(z);
}
}
Logical Implementation:-The Develope must analyse that what type of Exception was
occured and developer has to write logic to handle the Exception
using System;
class MyClass
{
static void Main(string[] args)
{
Console.WriteLine("Enter First no");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int y= int.Parse(Console.ReadLine());
if (y == 0)
{
Console.WriteLine("Denominator must not be 0");
}
else
{
int z = x / y;
Console.WriteLine(z);
}
}
}
observation:-it is very difficult for the developer to analyse that what type of
Exception was occured and write the logic to handle the exception
so Microsoft has given a mechanism to handle runtime Errors i.e try-catch
implementation
try
{
write the code here
}
catch(Excepttion e)
{
Display Error Message in catch block
}
finally
{
The code that we write inside finally will execute even exception occurs
}
only try - invalid
only catch - invalid
only finally - invalid
try-catch -valid
try-finally -valid
try-catch-finally -valid
catch-finally -invalid
try- multi catch -valid
try-catch inside try -valid
=================
Every Exception is a predefined class
class Exception
{
public virtual string Message{get;}
}
class DivideByZero:Exception class OverFLow:Exception class
FormatException:Exception
{ { {
public overridie string Message public overridie string Message
public overridie string Message
{ { {
get get get
{ { {
return "Attempting to return "Value is too large
return "i/p string was not in a
Divide by 0"; or too small ";
correct format";
} } }
} }
} } }
=====================================
whenever wn Exception will occur then CLR will identify the type of Exception anf
CLR will create object for corresponding
Exception class and CLR will thorw the object to the catch block
catch block will display Error message

using System;
class MyClass
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter First no");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int y = int.Parse(Console.ReadLine());
int z = x / y;
Console.WriteLine(z);
}
catch (Exception e1)
{
Console.WriteLine(e1.Message);
}
finally
{
Console.WriteLine("i will execute even exception occurs");
}
}
}
============================
UserFriendly Error Messages:-it is difficult for the ensuser to understand
Technical Error Messages
so that the developer must display userfriendly Error messages
using System;
class MyClass
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter First no");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int y = int.Parse(Console.ReadLine());
int z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException)
{
Console.WriteLine("Denominator must not be 0");
}
catch (FormatException)
{
Console.WriteLine("Plz enter number");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}

You might also like