Find and Replace Try/Catch Using Search Structurally

Answered

I feel like Search Structurally is a really powerful tool that I don't understand how to use.

I am trying to find instances where an Exception has been caught and the Logger has eaten the stacktrace - i.e. the Exception is not passed to the logger.

So far I have this:

try {
$TryStatement$;
} catch($ExceptionType$ $Exception$) {
$logger$;
}

Where $logger$; is a custom variable defined as: Log.error.*\"\)

This doesn't quite seem like the right way to do it but I can't follow the help.

Look at the help for this:

https://www.jetbrains.com/help/idea/2016.1/structural-search-and-replace-examples.html#search_in_comments_and_strings

Search in comments and/or string literals

Consider one wants to find comments or literal containing 'foo'. Search template would be like $SomethingWeWantToFind$ or "$SomethingWeWantToFind$". In case one wants to find comments/string containing some particular words (say, foo as a word), this should be specified as a text constraint.

 

What??

What is the difference between the quoted version and the unquoted version?

How does $SomethingWeWantToFind$ help us find 'foo'?

Lots of clicking of buttons made me realise that "text constraint" is something specified when you click "Edit Variables" but it isn't mentioned here.

This would be a lot clearer if you had lots more screenshots and step by step examples with screenshots of actual code that is found instead of random words like 'foo'.

 

 

 

0
2 comments

You probably want to use a query something like this:

try {
$TryStatement$;
} catch($ExceptionType$ $Exception$) {
Log.error("$string$");
}

$TryStatement$ min: 1 max: unlimited

Unfortunately this will only find try statements with one catch block, this is a limitation of Structural Search.

If it still doesn't work, please provide a concrete sample of the code you are trying to find.

 

Bas

0

That still didn't work. I put .*\"\) in the $string variable.

Sample code

Log.error(this, "Could not get filename");

Here is a slightly different version with an example

Sample code:

try {
//Some code;
} catch (Exception ex) {
ex.printStackTrace();
}

Test search:

try {
$TryStatement$;
} catch($ExceptionType$ $Exception$) {
$Exception$.printStackTrace();
}

That initially found nothing until I checked all the variables and found that my regex from $string had popped into $TryStatement

 

There needs to be some highlighting to indicate what are built in variables and what are user variables.

There should also be a list of what all the built in variables are and what they mean in the help files.

When a user variable is missing a constraint it should be highlighted as an error with a popup balloon saying "Click Edit variables to define this variable or add constraints" or something along those line.

0

Please sign in to leave a comment.