Alex Rivera | Logout

How can I avoid duplicated try catch blocks

Asked 2011-09-12T06:50:46.603
12

I have several methods that look like this:

public void foo()
{
   try 
   {
      doSomething();
   }
   catch(Exception e)
   {
      Log.Error(e);
   }
 }

Can I change the code to look like?

[LogException()]
public void foo()
{   
   doSomething();
}

How can I implement this custom attribute? and what are the pros and cons of doing it?

-----Edit 1------------

Can I implemented it myself, I mean just write one class, or do I need to use postsharp or another solution?

Edit
Report

1 Answer

3

You may try using: PostSharp

or try to google 'AOP' - 'Aspect Oriented Programming'. There are more similar techniques on the web.

answered 2011-09-12T06:56:23.107

Your Answer