views:

95

answers:

3

Hi all,

I would like to pose a question about developing and using attribute classes in your code.

I'm quite a newbie in developing attribute classes (it is an uncharted territory on my book!), and was thinking of creating an attribute class that will handle exceptions in methods instead of coding a try/catch() method in each method or class I create.

What would be the pros and cons of developing/using attributes in your classes? Would it be ideal to do use attributes in handling exceptions in your code?

Thanks a million!

Cheers, Ann

A: 

You might want to have a look at the exception policy mechanism in the MS Patterns and Practices Enterprise libraries.

I've not dealt to much with attributes, but my understanding was they were a better way than using marker interfaces to indicate that a class or method possessed a given property or should be used in a certain way.

Perhaps someone with more wisdom than I may have tried or thought about what you are suggesting and can give you a direct answer.

Spence
+8  A: 

Attributes, with a few exceptions with special treatment, only provide metadata. They don't provide executable code, unless you use a tool like PostSharp.

Personally, I'd code with try; it works, and won't confuse people.

Marc Gravell
Thanks for this!
mallows98
+2  A: 

This is basically what PostSharp's OnExceptionAspect class does.

It's a common desire to have an attribute to work with exceptions in many AOP frameworks.

Reed Copsey