Try Catch Trap

In any programming language you have the ways to handle exception built in your language. I will explain using C# language. Developer learns the mechanics of try catch finally. If you are reading this page you must be already competent on understanding the fundamental of exception handling. Go to msdn to brush it up.

I want to talk to a beginner developer about 2 points to keep in mind if you are creating your own way of handling exception for logging the errors with maximum and accurate information. Specially if you are not using any tools like Exception Handling Application Block

Point number one : Always use throw in side catch in all methods not call directly by GUI or Web Services. The reason is it will propagate the stack trace and you will get exact error line number.

Sample pseudo code:

//some method in helper class
public void SomeHelperMethod()
{
    try{
        //some code here
    }
    catch{
        throw;
    }
}

Point number two :

Always log the error in database or file at the outer most layer from where the first call is made. Thus giving details of class and methods through which the error is traversed.

Sample pseudo code:

//some web service class / code-behind code / controller code
Public void WebMethod()
{
    try {
        //some code
    }
    catch (Exception ex) {
        Log.write(ex)
    }
}

This might not be the most elegant solution but a simple to understand and helpful solution for DevOps team.

Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: