Processing Exception Using C # Attribute

I thought it was possible to handle exceptions using an attribute rather than writing "try ... catch" in each individual method.

For example, now each of my methods looks like this:

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

I want to:

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

Is it possible?

+6
source share
2 answers

This can be done using AOP (aspect-oriented programming). One framework for this is PostSharp. See Sample http://www.sharpcrafters.com/solutions/exception

+3
source

All Articles