|
exception handling |
EXCEPTION IN C#
“Exception is classes which is
responsible for the abnormally terminating of a program due to runtime error.”
We have two type of exception classes that's inherit exception as a base class. which is
1. System-Exception
2. Application-Exception
Why classes, see exception is not
a one class it having multiple class for every
error there is a class which responsible to handle that. There are hundred exception classes. Some System-Exception classes are.
ð Indexoutofbound exception
Index out of bound means we use a loop that
execute for 5 times but we start it from 0 and went to 5, now there are total
six iteration five will print our required result but how about the six one, here this exception class is responsible for the abnormal termination.
ð Dividebyzero exception
We know that no number can divide by zero
if we try to do such thing in our program here immediately the divide by zero
exception class will came into picture to cause the abnormal terminate of
program.
ð Overflow exception
If any value that
exceeding the size limit will give overflow.
ð Format exception
This class came into
role when you trying to convert a double type value into integer or may be
trying to convert string into double.
Exception handling
It is clear
that what is exception and no it’s time see how exception is occur and to
handle the exception means what we do when our program terminate abnormally
without give the required output in run time.
How the
exception is occur see we all know that our programs runs in supervision of CLR
inside when CLR found any mistake then it immediately make an instance that
similar to any exception and send to that exception class when this class run
our program terminate by displaying the relevant error message and exist flow
of control without executing the remaining statements.
Now once the abnormal
terminate occurred we have a mechanism called as exception handling
EXCEPTION HANDLING IN C#
“It is a process of stopping the
abnormal termination of a program whenever runtime error is occur in program.”
We can handle the exception
by putting the code into two blocks
ð
Try block
In order to handle the exception we make to separate block, Try block is the first block or also call fitrst step to handle the exception. After making algorithm of problems we differentiate the statement that may cause the run time error and write all that statement or block of code in try block.
ð
Catch block
Where the Catch block is used to write all those statement or block of code that should execute if the run time error is occur. Here we can display an user friendly massage or we also done an alternative action of the specific errors, such as if user enter an invalid value then by saying the actual value type we can regain the value.
|
try catch |
Like this
try
{
- Statements; //Here those statement require to
write that cause the error and those that doesn’t require to run if the error
occur.
}
catch (<exception class name><variable>)
{
-
Statements;
//here the statement will write that have to execute when only there is
a runtime error in program.
}
In catch block either you display a message
or done an alternative action for those error.