Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Exception using C# Attribute

Tags:

c#

attributes

I was thinking that is it possible to do exception handling using Attribute, rather than write "try...catch" in every single method.

for example, now, each of my method look like this:

public void DoSomething()
{
    try
    {
        // do something
    }
    catch (Exception ex)
    {
        // exception handling rules, always the same.
        throw;
    }
}

I want:

[HandleException]
public void DoSomething()
{
    // do something
}

Is it possible?

like image 911
Edi Wang Avatar asked Dec 27 '12 07:12

Edi Wang


1 Answers

This can be done via AOP (aspect oriented programming). One of the frameworks for doing so is PostSharp. See a sample http://www.sharpcrafters.com/solutions/exception

like image 172
avivr Avatar answered Oct 21 '22 19:10

avivr