When we develope of an
application,there are 2 type of error occoured in the Program.
1)Compile Time Error
2)Run Time Error
1) Compile Time Error:
The error that
come into picture due to syntatical mistake this error is called as Compile
Time error.These type of error can not take seriously because that error can
not effect on your Program.
2) Run Time Error :
Runtime Error come into
picture in the Middle of the program Execuation,due to Various Reason like
Wrong implementation of logic,or wrong input supply to the program,and Missing Required
Resources.
These type of error can take seriously because these error directly affect on program.
Exception Class:
“Exception class” is
Responsible for abnormal termination of program whenever runtime error
occour in program.
Object of this exception
class will take Responsible of abnormal termination after abnormal termination
they will also display error Message to stating reason of the termination.
Type of Exception:-
There are 2 type of exception
1)System Exception.
2)Application Exception.
System
Exception:-
Exception that gets raised implictly
of some predefind Condition is an
“System Exception”.
Eg.DivideByZeroException,FormatException,OverFlowException,NullReference
Exception
Application Exception:-
Exception
that gets raised explictly by the programmerin the application on own condition
is an “Application Exception”.
It can also reffered as “User Defind Exception”
How to Handle Exception:
When abnormal termination occour
in the program then remaining code of
the program cannot get execuated.if we want to handle these exception we need
to first stop abnormal termination and
take corrective action to resolve these problem.
Syntax:
The following
syntax can used for handling exception.
Try
{
//statement which causes
rruntime error
}
Catch( <Exception>
<var>)
{
//statement which execute
only when runtime error occour
}
Demo:-
1) Open visual studio 2012.
2) Select file -> New ->Project
3)
Select language as “visual c#” and template as
“Console Application”,give
appropritate name,I give name as ExceptionDemo.select Location as click on ok
4)by default name of the class is program.cs Rename it Demo.cs. by right click on program .cs
and choose rename give name as Demo .after they will ask conformation click ok.
5)Write the following code
7) Save the program and executie using f5.
If we provide y value as 0 then DivideByZeroException will occour and error
will be handle and display user friendly Message.
In the following output if we provide the non numeric value
then these exception will be handled by FormatException and show the user
Friendly Error Message.
On that way we can
handle abnormal termination.
Hope u enjoy my article.
For more article visit my blog
0 comments:
Post a Comment