views:

72

answers:

1

I need a Visual Studio regex to match swallowed exceptions.

This is as far as I got, but it still matches valid code like this:

catch (ArgumentNullException)
{
  //...
  throw;
}


catch:Wh*\(:Wh*.*Exception.*\):Wh*\{[:Wh*.*]*[^(throw:Wh*.*;)].*

How can I fix this?

Edit: Am I to take it from the comments that testing for the absence of a pattern in a regular expression in Visual Studio is not possible? - This is the thrust of my question. I would like to put aside questions of validity of approach (I am fully aware of FxCop et al, lexing and parsing, and this seminal post. I am also aware that swallowing exceptions is sometimes OK).

+1  A: 

I'm going to answer my own question here. This is an academic question, because, or course, there is a fundamental difference in the capabilities of a context free grammar and a regular expression as previous commenters have so eagerly pointed out, plus there are manifold existing non-regex -based tools that will perform this functionality for me.

I wanted to explore the negative lookahead capabilities of Visual Studio. According to reports in the comments here and here, the negative lookahead behavior of Visual Studio does not behave as you might expect under some circumstances (to the point of some calling it buggy).

I am not an expert on regular expressions, but i would expect the regex (throw) to match a line containing only throw - and indeed it does. I would also expect ~(throw) (this is the syntax for the negative lookahead in Visual Studio) not to match that same line - but, in Visual Studio, it does.

Having toyed with refining my original regular expression, including simplifying the test pattern, I come to the conclusion that performing negative lookaheads using the current implementation of Visual Studio's regular expression search is difficult, nigh impossible.

Ben Aston