Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to create a custom Code Analysis Rule Set in visual studio 2010, to identify how many places in the solution are not Handling Exceptions
(basically need to find out methods not having the Try, Catch block in my whole solution).

Please let me know if you need more explanation.
Can anyone help me!!!!

Thanks in advance.
Posted
Comments
joshrduncan2012 7-Jun-13 9:07am    
Help you with what? You haven't shown us any effort on your part yet as to what you have accomplished so far and where are you stuck.
Prince Sagayaraj 7-Jun-13 9:26am    
Hi

thanks for your reply!!!.

Are you aware of "visual studio 2010 code analysis"? (Sorry !! Don't take it in wrong side)

I am planning to implement the "visual studio 2010 code analysis" in our project to find out methods not having the Try, Catch block in my whole solution.

For that I need to create a "Custom Code Analysis Rule Set"

As I am new to the "visual studio 2010 code analysis" Tool. So am struck here to create a "Custom Code Analysis Rule Set"

Is this clear?
Sergey Alexandrovich Kryukov 7-Jun-13 10:42am    
You, too, please don't take me in wrong side.
Code analysis, static code analysis is a great thing, but how can you do it without understanding the very basic programming.
I doubt your understanding just because of the huge abuse you are trying to make. You have educate yourself well at least in this very basic but not trivial field of knowledge. Unlearn what you have learned.
—SA
Sergey Alexandrovich Kryukov 7-Jun-13 10:39am    
This discussion makes no sense at all, because the idea itself it totally harmful. It should never be done. What an abuse!
Please see my answer.
—SA
joshrduncan2012 7-Jun-13 10:41am    
I agree with you 100%.

You are going to totally abuse exception handling, effectively defeating its purpose. If you want to try programming where each method has try-catch, it would be much better to forget programming forever. This is not how exception handling works.

See, for example, my past answer:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^].

—SA
 
Share this answer
 
v2
Comments
joshrduncan2012 7-Jun-13 10:41am    
My +5.
Sergey Alexandrovich Kryukov 7-Jun-13 11:07am    
Thank you,
—SA
Maciej Los 7-Jun-13 12:11pm    
Very good reference ;)
Double 5!
Sergey Alexandrovich Kryukov 7-Jun-13 14:20pm    
Triple thank you, Maciej.
—SA
Prince,

Bad to see that your novice question was hammered by automated reply system.

Anyway, back to your question. Instead of wrapping all methods inside try-catch block one should look closely at the code inside methods and only put try-catches around lines of code that throws known exception. One should never swallow exceptions, don't just catch and rethrow, nor retry any other actions inside catch which might throw exception again. You should try to either recover from exception or simply log them to any output and then rethrow. Throwing again helps if you have a ErrorHanlders, Custom ServiceBehaviors, EnterpriseLibrary handlers, ELMAH implemented in each tier. They will take care of reporting unhandled exceptions. The 'finally' block is only needed when you want to cleanup resources disregard occurrence of the exception.

IMHO catching generic exception like "catch(Exception ex)" is not recommended generally. You may have N projects in the solution but you may only be deploying to 2 servers, e.g. App and Web, then you just have 2 tiers. Each tier should handle exceptions, log them etc and send a meaningful reply to the calling tier. Calling tier would just interpret that message and gracefully report to the user that something went wrong instead of crashing. Same should be applicable if exceptions occurs in the calling tier itself. If the calling tier is UI/Web people write handlers, common libraries or simply plug in ELMAH to report unhandled exceptions at a centralized location and redirect the user to Error page.

try-catch block is a programming practice, it does not hamper performance to use them but if you have too many that do nothing but catch and rethrow then there is a problem. It takes developer's own judgement to use that wisely in his design. Discuss with your architects and leads to find out if there is a guideline or standards document. If not then you will find some great articles here and elsewhere to get you started.

Best of luck...
 
Share this answer
 
v2
If you have layer architecture I would recommend you to throw exception and catch them in application layer and show in message box in presentation layer.
If you want to log them use Log4net or you can use Enterprise Application Block.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900