Saturday, September 20, 2008

How to handle try catches?

Many programmers hit there minds with this question. I considered yes and so I use to avoid using it but at final release to client , I found many issues raised by him and I am unable to replicate few of them , client is not able to guide me the way he got the error , some times the page (asp.net) showing “/Server Exception” which makes user irritated etc…

At last I believed this is all because of improper exceptional handling. I does not mean that if try catches are implemented these errors could not occur , but instead if you implement this exception handling by logging errors into a error log file in catch blocks, client can give you that error log file for reference and you can resolve the issues easily than expecting how they have come?



I felt 3 different ways to apply these try catches.
try
{
}
catch
{
throw;
}

(1)


try
{
}
catch(Excexption exp)
{
LogError(exp.Messagge);
throw exp;
}
(2)

try
{
}
catch
{
}

(3)


In the above mentioned cases
1) If you would not like to handle the exception or log it you can thow the expecption, such that be sure that if and only if you are throwing to a calling or parent function.
You cannot throw exception in a parent function instead if you would like to handle you can write (label.Text=exp.Message), which displays the error on to user interface instead having server exceptions or making user irritated.
Before showing error you can log that error into a text file.
Ex:-
public void btn_Calculate(object sender,Eventargs e)
{
try
{
//Since this function is called when user clicks on calculate button from UI
//i.e; the first function executed it is called parent function.
//Now this function calls Calculate() function which becomes child function.


int c=Calculate(a1,b1);
..
..

}
catch(Exception exp){
logError(exp.Message,” btn_Calculate”);
lblError.Text=exp.Message;
}
}
//This calculate function is a child function which is called by another function.
private int Calculate(a,b){
//Child function that has to be actually to handle exceptions.
try
{


return value;

}
catch (Exception exp)
{
logerror(exp.Message,”Sum()”);
throw exp;
}
}

private int Sum(a,b){

//Child function that logs error but returns -1 instead of throwing exception
try
{


return value;
}
catch (Exception exp)
{
logerror(exp.Message,”Sum()”);
return -1;
}
}
private int Sum(a,b){
//Child function that doesnot even log error but returns -1
try
{


return value;
}
catch (Exception exp)
{
return -1;
}
}
private int Sum(a,b){
//Child function that doesnot handle exception


return value;
//When there is any exception in child function , then the exception is raised in //parent function, so the error can be handled , but if there are multiple child //functions even error is handled at parent function but you cannot trace in //which function the error occurred unless you handle in each and log to error //log text file.
}
There are also specific exceptions like OracleException,SMTPException etc…
These Exceptions are child classes to a parent class called Excpetion.


So things like when you connect to oracle or retrieve data using datareader connected to oracle and send a mail to user, it could be good if you use OracleException,SMTPException objects.
try
{
}
catch(OracleException oraexp){
//This catch block holds error when oracle error ossurs
LogError(oraexp.ErrorNumber,”Excpetion due to oracle”);
}
catch(SMTPException smtpexp){
//This catch block holds error when smtp error occurs
LogError(smtpexp.innerExpetion.Message,”Excpetion while sending mail”);
}
Functions which you feel that they are perfect and 100% there is no way of throwing errors then you need not handle try catches.

Note : Finally blocks can also be appended which depends upon you function.

try
{
}
catch
{
}
finnally
{
//To dispose objects that are local to this function and imperative that the value or object
//returned to parent function should not be disposed :)
}

No comments: