Custom FxCop Rule Question
What type of rule am I trying to create?
In our projects we do not allow developers to throw exceptions inside a catch statement; instead they have to pass the exception to an ExceptionHandler which handles logging then rethrows the exception.
Maybe a rule like this already exists somewhere, I don't know.
Would love a sample.I am wanting to make this for Code Analysis in Team Foundation Server/Team System 2005.
Thanks in advance for your help, Jerry Nixon
It is very cumbersome to do this analysis, currently, and it's an area of the FxCop metadata API that will certainly change.
Having said that, you would want to parse the Method.Instructions. On encountering a _Catch instruction, you would remember the current location, then parse forward until you hit a matching _EndHandler instruction where the Instruction.Value refers back to the matching _Catch instruction. If you encounter a throw along the way, there's a problem. If you do not, continue parsing instructions from the original _Catch location. This approach will allow you to report properly if there's any nested try/catch within a catch handler.
We probably should write a sample that demonstrates doing this. I'll put it on the list.