-2
  1. I got "Uncaught SyntaxError: Invalid or unexpected token" error.
  2. And try to catch with 'try ~ catch' but it's not working.

    function a(){
      try{
       var img1 = "=="";  <#-- it occurs error -->
      }catch (e) {
        console.log("image error:" + e.message);
      }
    }
    
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
jumi lee
  • 15
  • 2
  • 5
  • 6
    Is that your exact code? Also you can't catch syntax errors (outside of eval at least). – TiiJ7 Jun 26 '18 at 07:55
  • I occurs error Intentionally to test catch statement. But it can't catch the error. – jumi lee Jun 26 '18 at 07:58
  • 1
    AFAIK syntax error is not exception – Terry Wei Jun 26 '18 at 07:59
  • Syntax errors are checked at the parsing time, `try .. catch` is executed at the running time, which will never take place because a syntax error has interrupted the script. – Teemu Jun 26 '18 at 07:59
  • As others mentioned, syntaxError will not be catch, instead try this http://www.javascriptkit.com/javatutors/error.shtml – Isaac Jun 26 '18 at 08:01
  • Then how to handle it? Actually, variable 'img1' is decryption string, but sometimes server send wrong string. – jumi lee Jun 26 '18 at 08:01
  • At first (if on your control) I'd tried to fix an occasionally occurring incorrect information at the server-side, or if that is not possible, you could rather ask how to fix the real problem at hands, not for a hack to fix it. – Teemu Jun 26 '18 at 08:04
  • You should correct the error cause by syntax error. Sounds like you generate this javascript by code, right? you should know how to make the javascript syntax correct. – Terry Wei Jun 26 '18 at 08:06
  • possibly a duplicate of [this question](https://stackoverflow.com/questions/5963045/can-syntax-errors-be-caught-in-javascript) – Aayush Sharma Jun 26 '18 at 08:10
  • 2
    Possible duplicate of [Can syntax errors be caught in JavaScript?](https://stackoverflow.com/questions/5963045/can-syntax-errors-be-caught-in-javascript) – Aayush Sharma Jun 26 '18 at 08:13
  • jumi lee, **please read**: [What should you do when someone answers your question?](https://stackoverflow.com/help/someone-answers) – Bharata Jun 26 '18 at 12:57

1 Answers1

4

You have a syntax error and you can not catch a syntax error.

This error is checked at the parsing or validation time of your code. The try .. catch statement is executed at the running time. And because of this it was not functioning.

If you eval or parse (for ex. JSON) your code you can handle syntax errors only. Or you can create a syntax error like this:

try {
  throw new SyntaxError('Hello', 'someFile.js', 18);
} catch (e) {

  console.log(e.message);  // "Hello"
  console.log(e.name);  // "SyntaxError"
}

For the eval handled or by self created syntax errors:

A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.

From MDN

Try this:

    function a(){
      try{
       var img1 = "==\"";
       //but you have to put some error here without parsing or validation error of your code.
      }catch (e) {
        console.log("image error:" + e.message);
      }
    }

I would like to recommend you read:

Bharata
  • 13,509
  • 6
  • 36
  • 50