6

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?

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

Also, you can write your own MSBuild Task. Here you can find an opensource project. During compile time it injects INotifyPropertyChanged supporting code in property setters using Mono.Cecil lib.

Comments

0

You can do this with Fody

https://github.com/Fody/Fody

There is a similar plugin to handle exceptions on async code if you want to check it.

https://github.com/Fody/AsyncErrorHandler

And here is one tutorial.

https://michielsioen.be/2017-10-21-il-weaving/

https://michielsioen.be/2017-12-28-il-weaving-pt2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.