Exception Handling
As we discussed whenever an exception comes into picture the
program terminate abnormally without executing the next lines of code even if
the code is no way related with the exception, so if we want to stop the
abnormal termination of program and still execute the code which is not related
with the exception being occurred we need the make use of exception
handballing.
- To handle an exception we must enclosed the code under some
spatial block known as try-Catch blocks.
try
{
- stmts which will cause a
exception
- stmts which will not be
executed when exception occurs
}
catch (<exception><var>)
{
- stmts which should be
executed only when an exception occurs
}
|
Multiple Catch Blocks
when we enclosed the code
under try and catch blocks, the execution of the program will be as following:
- If all the statement in try is executed successfully without executing the catch block from the last stmt of try the control directly jumps to the stmt which is present after catch blocks.
- If any of the stmt in try comes rises or causes an exception from that line of code without executing any stmts in try control jumps to catch block searching for a matching catch block. If a matching catch block is avilabel executes the stmts under that catch block and than jumps to the stmt after all the catch blocks, if the matching catch block is not avilabel abnormal termination occurs again.
- Add a class ExcepDemo.cs and write the following:
class ExcepDemo
{
static void
{
int x, y, z;
try
{
Console.Write("enter x value : ");
x = int.Parse(Console.ReadLine());
Console.Write("enter y value : ");
y = int.Parse(Console.ReadLine());
z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException ex1)
{
Console.WriteLine(" Divisor can't be zero");
}
catch (FormatException ex2)
{
Console.WriteLine("Input must be integer");
}
catch (Exception ex3)
{
Console.WriteLine(" error occurred ");
}
Console.WriteLine("end of program");
Console.ReadLine();
}
};
}
|
Finally Block
(V. imp):- in a block of code which gets executed both
the times when an exception occur or did not occur also.
using System;
namespace oopsProject
{
class FinDemo
{
static void
{
int x, y, z;
try
{
Console.Write("enter x value : ");
x = int.Parse(Console.ReadLine());
Console.Write("enter y value : ");
y = int.Parse(Console.ReadLine());
if (y == 1)
return;
z = x / y;
Console.WriteLine(z);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("finally block");
}
Console.WriteLine("end of program");
Console.ReadLine();
}
}
}
|
- In the above program if at all the value to the deviser is given
as 1 the execution of the program stops their because we are using return to
jump out of the method but all this happens only after executing finally block,
because once the control enters into try without executing finally we cannot
stop the program execution.
Note –
Massage is a property of the exception class which returns the predefined error
massage associated with the currently occur exception.
- Massage is a virtual property of class exception which is
overridden under all the child exception classes according to their recurrent,
so the reference of the exception class holding object of its child class
(Currently occur Exception) was able to access property massage of its child
class and display the relevant error massage.
Try catch and finally statement can be used in three different combinations:
- Try and Catch – in this case abnormal termination of the program stops as we are handling the exception.
- Try Catch and Finally – Execution will be same as above as well as all the stmts in finally gets executed at any cast.
- Try and Finally – In this case abnormal termination will not stop because we are not handling the exception using catch, but all the stmts is finally will be executed.
Exceptions are two types
- System exception.
- Application exception.
- An exception which gets raised implicitly on some pre-defined
error conditions is a system exception. Eg: Format exception divides by zero
exception etc.
- An exception which gets raised explicitly on user defined
condition is an application exception can also be called as user defined
exception.
0 Comments