Using attributes to wrap methods with exception handling

Last post 11-20-2007 3:46 PM by vcsjones. 5 replies.

Sort Posts:

  • Using attributes to wrap methods with exception handling

    11-16-2007, 6:13 PM
    • Member
      439 point Member
    • jshallard
    • Member since 08-11-2003, 7:44 AM
    • Gulf Breeze, Florida
    • Posts 171

    I would like to write a custom attribute that i can decorate method with, that will essentially "wrap" it in a try/catch block.  I am sure i have seen an article on how to do this - but in can't find it!  If anyone could point me towards an article on how to do this, or give me some pointers on how I can go about this, it would be much appreciated.

    Thanks for any help
     

  • Re: Using attributes to wrap methods with exception handling

    11-17-2007, 8:38 AM
    • All-Star
      18,335 point All-Star
    • Svante
    • Member since 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 2,298
    • Moderator

    This seems like a very unusual idea. You can't really do it with just custom attributes - attributes are just meta data emitted into the assembly. They must be checked by other code for something to happen. You could perhaps subvert the use of a SecurityPermission-derived attribute to have .NET call your code when a method is decorated with such an attribute. I don't off-hand see how you could wrap a method in a try-catch-block that way - nor do I see when that would be a good idea.

    If someone has more information I'd be interested to see just why anyone would want to do that. 

    Just what are you trying to achieve? It might be that there's an alternative way to do this, or perhaps you should not be doing it at all? 

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: Using attributes to wrap methods with exception handling

    11-17-2007, 10:27 AM
    • Member
      439 point Member
    • jshallard
    • Member since 08-11-2003, 7:44 AM
    • Gulf Breeze, Florida
    • Posts 171

    As for "why", well some would call it lazy, and some would call it efficient!  But, basically, in my web layer, around almost all my top-level methods (event handlers, onload, oninit etc), I have the same code over and over: 

    	try
    {
    ...
    }
    catch (Exception ex)
    {
    App.PageManager.HandleException(ex);
    }

     It would be nice to be able to instead just add an attribute (say "[DefaultExceptionHandling"]) to these methods for two reasons:

    • less code to write
    • It would be possible to change the default exception handling in one place, and have it apply everywhere.

    This is kind of getting into "aspect oriented programming" / "Code Injection" area, which i have heard a little about, but do not have any experience in.

    If anyone has any info on how i could achieve the above, it would be much appreciated.

    Thanks

     P.S. And before anyone says it, no i don't want to just handle the error in the OnError event.  This stops the rest of the page processing - which i do not want to do... and besides, i know want to know how to "wrap" code! Big Smile
     

     

  • Re: Using attributes to wrap methods with exception handling

    11-20-2007, 9:44 AM
    • Member
      439 point Member
    • jshallard
    • Member since 08-11-2003, 7:44 AM
    • Gulf Breeze, Florida
    • Posts 171

    For all those who are interested...

    I never did find that original post, but i did find a way to do what i want.  I used an open source library called "PostSharp" which (amongst other things) allows you to develop attributes which "inject" code at compile time in an AOP stylee.  I am fairly excited about the possibility to remove a lot of the repetitive junk in may of my methods using this library:

     

    PostSharp Library: http://www.postsharp.org/ 

    PostSharp forum thread about exception handling: http://www.postsharp.org/community/forums/laos/479123478

     

    I hope this is useful!


     

  • Re: Using attributes to wrap methods with exception handling

    11-20-2007, 11:47 AM
    • All-Star
      18,335 point All-Star
    • Svante
    • Member since 02-12-2007, 12:15 PM
    • Stockholm, Sweden
    • Posts 2,298
    • Moderator

    jshallard:
    "PostSharp" which (amongst other things) allows you to develop attributes which "inject" code at compile time
     

    Pretty neat.

    Just for the record though - it does not inject code at compile time. It's not just a library. It injects code after compilation. It's a MSIL post-processor (guess why it's called PostSharp ;-). It actually hacks the compiled code and adds new MSIL code to the assembly. In this sense it's related to obfuscators, which also works after compilation and modifies the output from the compiler.

    It's not really any different from a macro pre-processor, which is another way to enhance languages with these kinds of features. 

    It probably works pretty well, although I'm always wary about stuff which can break assumptions in other tools and parts of the environment such as the JIT-compiler, the garbage collector etc.

    Anyway, thanks for posting the link.
     

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: Using attributes to wrap methods with exception handling

    11-20-2007, 3:46 PM
    • All-Star
      33,841 point All-Star
    • vcsjones
    • Member since 04-18-2006, 4:53 PM
    • Falls Church, VA
    • Posts 4,319
    • Moderator
      TrustedFriends-MVPs

    You should be careful with post-op processors, like that one and obfuscators. They typically rip off the Strong Signing of your assemblies, so you need to resign it using the sn.exe tool, or some of the tool can do it themselves.

    Cheers,
           Kevin Jones


Page 1 of 1 (6 items)