Results 1 to 6 of 6

Thread: Problem in handling exception with Regex

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    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.
    Last edited by Gunner 1; 16-02-2010 at 01:49 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Problem in handling exception with Regex

    Hello,
    I think you can try the following, to do the same. Just try it
    Code:
    catch[^\}]*throw
    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.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Problem in handling exception with Regex

    Hello,
    A small note, do not forget escaping character ')':
    Code:
    String rg = "catch [^\\)] .* * throw "
    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.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    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.

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    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.
    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.

Similar Threads

  1. Exception Handling in .Net
    By Bruno007 in forum Software Development
    Replies: 5
    Last Post: 25-12-2010, 01:30 AM
  2. Exception Handling in Singleton
    By Bottlenecked in forum Software Development
    Replies: 6
    Last Post: 20-09-2010, 09:14 PM
  3. Exception handling in PL/SQL
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 11:04 PM
  4. Exception handling in EJB
    By KennedII in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 04:40 AM
  5. Exception Handling in Csharp
    By Jesus2 in forum Software Development
    Replies: 3
    Last Post: 11-11-2009, 11:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,817,760.51109 seconds with 16 queries