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.
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.
EXAMPLE OF EXCEPTION HANDLING IN C#
Note: it is
mandatory to check our program’s statement by statement and see that which
statements will be responsible to raise runtime error, so that’s statements must
put into try block and all those statement also that need to execute when error
is occur, and check which statement should execute if there runtime error will
occur and put all that statement in catch block if there only one exception
that can raise then we will define only one catch block otherwise if many type
of exception would occur then number of catch block should be define by
specific exception class name and massage that should display. At the end if
you have doubt that may be some exception can rise other than we define in
program in such a case you have to define and exception catch block to handle
those exceptions see the example..
Let’s write
a program that take two inputs and divide it, and use exception handling
Solution
Make the draft solution and check
the exception in the above problem there may be three type of exceptions that’s
are
ð FormatException
ð IndexOutOfRangeException
ð DivideByZeroException
Which is used in the solution see it
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("enter a
number");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("enter second
number");
int b = int.Parse(Console.ReadLine());
double c = a / b;
Console.WriteLine("the division
is = {0}",c);
}
catch(FormatException ex1)
{
Console.WriteLine("the input must
be a integer value");
}
catch(IndexOutOfRangeException ex2)
{
Console.WriteLine("the range must
be 8 digits");
}
catch(DivideByZeroException ex3)
{
Console.WriteLine(ex3.Message);
}
catch(Exception ex4)
{
Console.WriteLine(ex4.Message);
}
Console.WriteLine("end of the
prorgam");
Console.ReadKey();
}
}
0 comments:
Post a Comment
Please do not enter any spam link in the comment box