Problem in handling exception with Regex
Hello,
I want to identify via a java REGEXP the following snippet:
Code:
catch (Exception e) {
// ...
throw e;
}
I think it comes down to having a catch, followed by a throw before a closing brace is found. On or regexp: "[.&&[^}]]* catch throw" which means the word "catch", then any character except ")" 0 or n times and then throw. But it does not work. Shortly, I am trying to handle exception with the Regex. If you have any idea about it then please post back. Thanks in advance.
Re: Problem in handling exception with Regex
Hello,
I think you can try the following, to do the same. Just try it
which means "catch" followed by any character other than ")" followed by "throw". It works better? I have tried this with my code, though not sure that it will work for you , because everybody program and the output are different.
Re: Problem in handling exception with Regex
Hello,
Perfect indeed, but we must add a .* at the end if the string contains characters after the throw:
Code:
System.out.System.out.println(Pattern.games("catch throw [^}]* .*", "catch (Exception e) (e.printStackTrace () throw e;)));
And this
Code:
System.out.System.out.println(Pattern.games("catch throw [^}]* .*", "catch (Exception e) (e.printStackTrace ();) throw e;"));
It remains for me to recognize the opposite, namely a catch that does not throw.
Re: Problem in handling exception with Regex
Hello,
A small note, do not forget escaping character ')':
Code:
String rg = "catch [^\\)] .* * throw "
Quote:
It remains for me to recognize the opposite, namely a catch that does not throw.
He'll just change your first regular expression to make the "throw" option (with the quantifier ?) And you have one regular expression :
Code:
String rg = "catch [^ \ \)] * (throw)?.*"
Hope this is going to help you.
Re: Problem in handling exception with Regex
Hello,
The character ')' need not be escaped, it seems. In addition to "\ \" is escaping the '\' => As for me, it seems to walk in both cases with or without your exhaust.
Code:
He'll just change your first regular expression to make the "throw" option (with the quantifier ?) And you have one regular expression :
Code:
String rg = "catch [^ \ \)] * (throw)?.*"
Indeed, but I recognize the words or the throw is not and only them. This is not optional. From what I read here and there, this is not possible via a regexp, it must be code, which is not possible in my case. Negation '^' can be used on the characters: "[^ abc]" means either a or b or c. But that does not work for a group of letters.
Re: Problem in handling exception with Regex
Hello,
If you look at the javadoc of the class Pattern You will notice that several expressions using braces ( '(' and ')') as special characters, it's better to take precaution to avoid.
Quote:
In addition to "\ \" represents the exhaust '\
Why do I need to double / quadruple backslash character ( '\')? I think this is a good option to try, just try it and post what you get the output of the program and if there are more queries then we will try to solve them.