Exception Handlings in .net..?
In this article I'm trying to explain what are the exceptions and different types of exceptions and working with multiple catch blocks and working with try catch and finally blocks with small small examples. And discussing with User Defined exceptions and system exceptions.
Exception Handlings:
In this article I'm trying to explain what are the exceptions and types of exceptions and working with multiple catch blocks and working with try catch and finally blocks with small small examples.
As we have learnt that when ever an exception occurs our program terminates abnormally with out the next lines of code, even if the next lines of code is not related to exception. so if we can stop the abnormal termination of program we can make the statements which are not related with the exceptions to execute.
If we want to handle an exception our code should be enclosed under some special blocks.
i.e. try, catch block which has to be used as following. Try, Catch Blocks:
Try
{
-statements which will cause an exception
-statements which doesn't require exception when exception occurs.
}
-------multiple catch blocks.
When the code is enclosed under try and catch blocks the execution of program takes place the following.
If all the statements in try are executed successfully from the last statement of try the control directly jumps the first statement which is present after catch block without executing any catch block.
If any statement in try faces (or) comes across an exception from that line the control directly jumps to the catch blocks checking for a matching catch block. If a matching block is available abnormal termination stops there executes the code under the catch block and then jumps to the first statement after catch block.
If matching catching block is not available the abnormal termination occurs again.
EX: Add a class Demo.cs
Class Demo
{
Static void main()
{
int x, y, z;
try
{
Console.WriteLine ("enter x value");
x=int.Parse(Console.WriteLine());
Console.Write("enter y value");
Z=x/y;
Console.WriteLine (z);
}
Catch (Divison by zero Exception ex)
{
Console.WriteLine ("divisor can't zero");
}
Catch (FormatException ex)
{
Console.WriteLine ("input must be integer");
}
Catch (Exception ex)
{
Console.WriteLine ("Error occurred");
}
}
}Finally Block:
This is another block of code which can be paired with try and catch.
The specialty of this block is it executes both when an exception occurred
or did not occurs also.
EX:
Add a class Finaldemo.cs
Class Finaldemo
{
Static void main()
{
int x,y,z;
try
{
Console.Write("enter x value");
x=int.Parse(Console.WriteLine());
Console.Write("enter x value");
x=int.Parse(Console.WriteLine ());
if(y==1)
return;
z=x/y;
Console.WriteLine (z);
}
Catch(Exception ex)
{
Console.WriteLine (ex.message());
}
finally
{
Console.WriteLine ("Final block");
}
Console.WriteLine ("end of the program");
}
}
In the above program if the value to the divisor is gives as one the exception of program steps because return statement will jump to control out of the method. But all this happen only after execution of finally block. Because once control enters into try without executing finally we can't stop the execution of a program.
Message is a virtual property of exception class which returns the error message. i.e. associated with currently occurred exception.
As message is a virtual property that can be overridden any predefined class exception will override the message property to provide the error message specific to the exception.
Message is a read-only property which contains only the get block in it.so predefined exception will be internally defined as following.
Prototype:
Public class
{
Public override string message
{
get{return "
}
}
Note:
The above prototype used to, if required you can also define our own exception classes.Try, Catch and Finally:
These three blocks can be used in3 different combinations.
1.try and catch:
Here any exception that in a program gets handled.
2.try ,catch and finally:
There will be same as above but all the statements in the finally block gets executed at any cost.
3.try and finally:
Here exceptions are not handled when they occur so abnormal termination will not stop. But even if the program is abnormally going to terminated finally block gets executed.Types of Exceptions:
Exceptions are of two types:
1.System Exceptions(Pre defined)
2.Application Exceptions(User defined)
An Exception which gets raised implicitly on some predefined error conditions is an system exceptions .
Ex:-
Divide by zero, file not found exceptions
An exception which gets raised by the programmer specific to his application on any user defined condition is an application on Exception. These are purely user defined Exceptions.
How can a programmer raise an exception:
If the programmer wants to raise an exception he can follow the below process.
1.Create the object of an exception class.
FormatException ex=new FormatException();
2.now throw the object using throw statement
throw
throw ex;
(or)
throw new FormatException();
If we want to raise our own exception within a program we can follow any of these processors.
1. Create object of application exception class by providing the error message it has to display whenever an exception is raises as a parameter to its constructor.
ApplicationException (
2. create our own exception class by giving our own name to it for our requirements following the prototype of our class we have same as above.
Public class < Exceptionname>: Exception
{
Public override string message
{
get {return "msg" ;};
}
}
EX:Add a class throwdemo.cs
namespace oopsprogect
{
Class oddnumberException: Exception
{
Public override string message
{
Get
{
return "divisor cannot be an odd number";
}
}
}
Class throwdemo
{
int x, y,z;
Console.WriteLine("enter x value");
x=int.Parse(Console.WriteLine());
Console.WriteLine("enter y value");
y=int.Parse(Console.WriteLine());
if(y%2>0)
{
//throw new oddnumberException();
throw new application Exception("divisor can't be an odd number:");
}
z=x/y;
Console.WriteLine(z);
Console.WriteLine("end of the program");
}
}
Consider below simple code for the need of Exception Handling
using System;
namespace ExceHand
{
class Example
{
static void main()
double e = Divide(8,5);
Console.WriteLine (e);
}
static double Divide(double x,Double y)
{
double r=x/y;
}
}
}
In the above code Divide() returns the result of division . We passed two number in parameter