Open In App

Unexpected token error for catch JavaScript

Last Updated : 11 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will try to understand the fact that why we receive Unexpected token errors while we are dealing with try/catch in JavaScript, and with the help of certain coding examples (in JavaScript itself) we will try to see how we may be able to resolve such token error properly.

Unexpected token error is usually regarded as a subset error of the main Syntax error which generally appears whenever we try executing a code snippet in JavaScript with either an extra or missing character in the original syntax itself and also this Syntax error object is generally inherited from the main object termed as Error object. 

Even if we are not dealing with try/catch block execution, this error may be witnessed with any other method or syntax execution only if something from that particular syntax seems missing or something extra apart from the required parameters in the syntax has been added.

Let us have a look over the below-illustrated syntax which is used to describe a try/catch block in JavaScript.

try {
    // In this block all the executable stuffs has to be added...
    // which could be error free or with error too..
}

catch(error){
    // In this block error catching as well as
    // handling is performed...
}

After analyzing the above syntax let us have a look over the below-enlightened example in which we will showcase how this Unexpected token error arises and then laterwards in another example we will showcase its solution too.

Example 1: In this example, we will omit the error parameter in the catch block syntax, and without that, we will try to execute our code, and eventually at the end we will see the error coming due to the same task done.

Javascript




<script>
    let execution = () => {
        try {
            let a = 10;
            let b = 20;
            let sum = a + b;
            console.log(sum);
        }
        catch () { }
    }
  
    execution();
</script>


Output:

catch(){}
      ^
SyntaxError: Unexpected token ')'
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

Example 2: In order to resolve the error caught in the above example we simply have to add one error parameter inside the catch() method and adding that will actually resolve the error and displays the result.

Javascript




<script>
    let execution = () => {
        try {
            let a = 10;
            let b = 20;
            let sum = a + b;
            console.log("Sum is : " + sum);
        } catch (error) {
            console.log(error.message);
        }
    };
  
    execution();
</script>


Output:

Sum is : 30


Previous Article
Next Article

Similar Reads

three90RightbarBannerImg
  翻译: