Javascript Try/Catch

13,945

Solution 1

Try this the new RegExp is throwing the exception

Regex

    <script type="text/javascript" charset="utf-8">
            var grep;

            try {
                    grep = new RegExp("gr[");
            }
            catch(e) {
                    alert(e);

            }
            try
            {
                    var results = grep.exec('bob went to town');
            }
            catch (e)
            {
                    //Do nothing?
            }

            alert('If you can see this then the script kept going');
    </script>

Solution 2

The problem is with this line:

var grep = new RegExp('gr[');

'[' is a special character so it needs to be escaped. Also this line is not wrapped in try...catch, so you still get the error.

Edit: You could also add an

alert(e.message);

in the catch clause to see the error message. It's useful for all kind of errors in javascript.

Edit 2: OK, I needed to read more carefully the question, but the answer is still there. In the example code the offending line is not wrapped in the try...catch block. I put it there and didn't get errors in Opera 9.5, FF3 and IE7.

Solution 3

var grep, results;

try {
    grep = new RegExp("gr[");
    results = grep.exec('bob went to town');
}
catch(e) {
    alert(e);
}
alert('If you can see this then the script kept going');

Solution 4

putting the RegExp initialization inside the try/catch will work (just tested in FireFox)


var grep, results;

try
{
    grep = new RegExp("gr["); // your user input here
}
catch(e)
{
    alert("The RegExpr is invalid");
}

// do your stuff with grep and results

Escaping here is not the solution. Since the purpose of this snippet is to actually test a user-generated RegExpr, you will want to catch [ as an unclosed RegExpr container.

Solution 5

your RegExp doesn't close the [

In my FireFox, it never returns from the constructor -- looks like a bug in the implementation of RegExp, but if you provide a valid expression, it works

Share:
13,945

Related videos on Youtube

Teifion
Author by

Teifion

I am a Software Engineer in a UK Car Insurance provider. I mainly work in Python and PHP.

Updated on June 04, 2022

Comments

  • Teifion
    Teifion almost 2 years

    I've got a function that runs a user generated Regex. However, if the user enters a regex that won't run then it stops and falls over. I've tried wrapping the line in a Try/Catch block but alas nothing happens.

    If it helps, I'm running jQuery but the code below does not have it as I'm guessing that it's a little more fundamental than that.

    Edit: Yes, I know that I am not escaping the "[", that's intentional and the point of the question. I'm accepting user input and I want to find a way to catch this sort of problem without the application falling flat on it's face.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
        <title>Regex</title>
    
        <script type="text/javascript" charset="utf-8">
            var grep = new RegExp('gr[');
    
            try
            {
                var results = grep.exec('bob went to town');
            }
            catch (e)
            {
                //Do nothing?
            }
    
            alert('If you can see this then the script kept going');
        </script>
    </head>
    <body>
    
    </body>
    </html>
    
    • Teifion
      Teifion over 15 years
      I did proofread it, at least 4 times. It's a case of someone else being so much better at spotting that silly mistake you make, you may have come across it yourself.
    • Andreas Grech
      Andreas Grech over 13 years
      @Geoffrey: Actually, it's JavaScript.
  • Teifion
    Teifion over 15 years
    You've missed the question itself
  • Teifion
    Teifion over 15 years
    As with rslite, you've missed the actual question, I want to find a way to run such a regex and catch the resultant error
  • Teifion
    Teifion over 15 years
    Yeah, I've seen that now in Paul's answer. It was a simple case of me being extra stupid. Thanks for the help anyway :)