tags:

views:

93

answers:

1

I am dealing with a large old codebase that has a lot of these:

try
{
    ...
}
catch
{
    throw;
}

Resharper helpfully marks these and offers the option to "Remove redundant catch", but I don't like having to go through one-by-one, I would like to wipe them out all at once. Is there a way to do that with Resharper, possibly by integrating it in with "Code cleanup"?

+2  A: 

If you are using Resharper 5.0 you can import the following pattern (save it as an xml file) into the Pattern Catalog (Resharper>Tools>Pattern Catalog>Import) then run Search Now to find all instances so that you can review and choose to perform the replacement on them.

<CustomPatterns>
  <Pattern Severity="HINT">
    <Comment>useless catch</Comment>
    <ReplaceComment>remove useless catch</ReplaceComment>
    <ReplacePattern>$code$</ReplacePattern>
    <SearchPattern><![CDATA[try
{
    $code$
}
catch
{
    throw;
}]]></SearchPattern>
    <Params />
    <Placeholders>
      <StatementPlaceholder Name="code" Minimal="1" Maximal="-1" />
    </Placeholders>
  </Pattern>
</CustomPatterns>

The pattern worked on my test code. I don't know how the Pattern matching engine handles whitespace so you may have to change tabs to spaces or whatever your code standard is as appropriate.

Handcraftsman
Thanks Handcraftsman! Just to check, I emailed JetBrains and they told me the same thing: "there's no way to apply a certain quick-fix (such as 'Remove redundant catch') solution-wide, but you can use the new Structural Search and Replace functionality to search and replace such constructs."A little walk-through is on their blog:http://blogs.jetbrains.com/dotnet/2010/04/introducing-resharper-50-structural-search-and-replace/
David McClelland

related questions