1

In a Visual Studio 2010 C# Project, how do I find out if any method is written without having a try-catch block?

Sometimes as a code reviewer, its hard to search function/methods which are not properly written according to code standards esp: without a try-catch block.

I know that the Find option in Visual Studio supports regular expressions. But what's the regular expression that could perform this job smartly?

Bruno Brant
  • 8,226
  • 7
  • 45
  • 90
Mazhar Karimi
  • 157
  • 1
  • 10
  • How does having Try/catch help you find if a method is written? interested to see. – MBen Jul 04 '12 at 16:40
  • 3
    I believe the author is asking how to find methods which don't have try/catch blocks in them. – Peter T. LaComb Jr. Jul 04 '12 at 16:40
  • 6
    Of course, having a try/catch block in a method isn't always required or even helpful, so no upvote on the question from me. – Peter T. LaComb Jr. Jul 04 '12 at 16:41
  • Yes, Peter you got it. I actually edited the question and added more description. And why -1?? – Mazhar Karimi Jul 04 '12 at 16:42
  • Peter: may be not helpful to you, but according to my job and task its important. :) – Mazhar Karimi Jul 04 '12 at 16:43
  • 2
    I didn't vote either way - I dont' think it's a good question, but it's not so bad as to deserve a -1. I think it will lead more to discussion of exception handling practice which is well covered elsewhere. – Peter T. LaComb Jr. Jul 04 '12 at 16:43
  • Regular expression to search for this? How would you determine using regex which code belongs to try-catch block and which doesn't? – walther Jul 04 '12 at 16:45
  • @Mazhar Karimi, no. **Trying to ensure every method has a try-catch block is terrible practice**. See this question: https://stackoverflow.com/questions/14973642 – MGOwen Jun 04 '18 at 09:31

3 Answers3

6

Edit (putting the direct answer first): It would actually be easy to use Assembly.ReflectionOnlyLoadFrom, then enumerate the types, and the methods of those types, then for each method body examine the ExceptionHandlingClauses.

Commentary follows:

Red Gate used to offer the Exception Hunter tool for tracking down possible exception issues. As mentioned on that page, it's been shown that the specific task you have requested (broad searches for any unhandled exception) does not lead to higher quality software even with assistance of automated analysis tools.

If I needed to find a list of all methods without a protected region, I could simply use one of my experimental assembly loaders (written with relative ease according to ECMA-335) and examine the metadata as described in ECMA-335, Partition II, §25.4.5 and §25.4.6 (much easier to use the first part of this answer). If I needed to find a list of these methods for the purpose of adding a protected region to each of them to conform to a coding standard, I would report back that they need to find another developer to do that because I refuse to knowingly and intentionally degrade the quality of software I work on.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 1
    *I would report back that they need to find another developer to do that because I refuse to knowingly and intentionally degrade the quality of software I work on.* +1 – DaveShaw Jul 04 '12 at 16:58
1

If you have access to Visual Studio 2012 (the beta is a free download!), you can use the Search For Code Clones option.

Normally this will search your whole solution but you can scope it to a specific piece of code. You can easily use this to find all code where, for example, a disposable object is used. Then you can chek if a using statement is used everywhere.

This same mechanism can be used to check if a certain piece of code is used in a try/catch block.

Here is some MSDN documentation.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
0

I think you could use Roslyn to process the code and find which methods have or don't have try/catch blocks by examining the expression tree.

I would not advise you to use Regex to do that. You'd have to write a quite complex script that would be a state machine based on when a method block starts and when it finishes, and then inspecting whether are try-catches in there.

Bruno Brant
  • 8,226
  • 7
  • 45
  • 90
  • that... I already know and mentioned in question, what would be the regular expression, that's a question. :) – Mazhar Karimi Jul 05 '12 at 06:54
  • @MazharKarimi, I guess you didn't understood my answer. I *do not* advise using Regex to do that. It would be quite complex, maybe more than 40 manhours -- which I don't have. I gave you an alternative, Roslyn. Hope it helps. – Bruno Brant Jul 05 '12 at 18:59