Can't find try catch with patterns

Hi all,
I'm tearing my hair out with patterns. They are such a pain to work with.

I'm trying to find and replace all instances of the following

            try
            {
                // Lots of statements
            }
            catch (MyException e)
            {
                throw e;
            }
            catch (Exception ex)
            {
                // More statements
            }

so I can bulk replace the line highlighted with something else.

There may be other ways of doing this, but I really want to understand why I can't make a pattern to find this.

I'm running Re#er 6, but I'm sure the patterns from other versions would work.

The pattern
try
{
$statements$
}
catch($args$)
{
$morestatements$
}

finds basic try..catch statements, but

try
{
$statements$
}
catch($args$)
{
$morestatements$
}
catch($moreArgs$)
{
$evenmorestatements$
}

finds nothing, and I need to be more precise about the type used in the args.


Please help.

Colin

3 comments
Avatar
Andrey Serebryansky
Comment actions Permalink

Hello Colin,

I've checked this behavior and the following pattern:

try
{
  $stmt$
}
catch(IOException $eid$)
{
  $stmt1$
}
catch(Exception $eid1$)
{
  $stmt2$
}

correctly finds the following try/catch statement:

try
{
       Console.WriteLine("Test");
}
catch (IOException ex)
{
       throw ex;
}
catch(Exception e)
{
       Console.WriteLine(e);
}

Let me know if this helps. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi all,
I'm tearing my hair out with patterns. They are such a pain to work
with.
I'm trying to find and replace all instances of the following

try
{
// Lots of statements
}
catch (MyException e)
{
throw e;
}
catch (Exception ex)
{
// More statements
}
so I can bulk replace the line highlighted with something else.

There may be other ways of doing this, but I really want to understand
why I can't make a pattern to find this.

I'm running Re#er 6, but I'm sure the patterns from other versions
would work.

The pattern
try
{
$statements$
}
catch($args$)
{
$morestatements$
}
finds basic try..catch statements, but

try
{
$statements$
}
catch($args$)
{
$morestatements$
}
catch($moreArgs$)
{
$evenmorestatements$
}
finds nothing, and I need to be more precise about the type used in
the args.

Please help.

Colin

---
Original message URL:
http://devnet.jetbrains.net/message/5387572#5387572



0
Comment actions Permalink

Thanks, don't know why the args version didn't work.

So the Find pattern works, but the problem is when I use the replace functionality, it's stripping all the comments out of my statement sections.

How do I stop that?
I'm using Find pattern of

try
{
  $stmt$
}
catch(MyException $eid$)
{
  throw $eid$;
}
catch(Exception $eid1$)
{
  $stmt2$
}

and a replace pattern of

try
{
  $stmt$
}
catch(MyException)
{
  throw;
}
catch(Exception $eid1$)
{
  $stmt2$
}

Surely it should leave comments in with the statements?

Colin

0
Comment actions Permalink

In Replace mode it constructs new code snippet from given replace pattern, without any comments (because in replace pattern itself there no comments).
In statement placeholders comments are skipped too, unfortunetaly.
Here is related issue in our tracker: http://youtrack.jetbrains.net/issue/RSRP-274520

0

Please sign in to leave a comment.