Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Catch or Finally After Try

I'm using Node, Express with EJS view engine, nano (for couchdb), and I'm running into this pretty puzzling error which I cold not find any Node/JS specific info about via SO or Goog. The area of my code that seems to be triggering this involves nested async callbacks which grab a document from CouchDB, look through it for some matches, and push an object to an array in my private scope.

And the error:

Express
500 SyntaxError: Missing catch or finally after try
at Object.Function (unknown source)
at exports.compile (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:234:12)
at Object.exports.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:273:10)
at View.exports.renderFile [as engine] (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/ejs/lib/ejs.js:303:22)
at View.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/application.js:501:10)
at ServerResponse.res.render (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/express/lib/response.js:719:7)
at exports.dashboard.res.render.msg (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/routes/index.js:19:29)
at module.exports.read (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/api/Character.js:56:25)
at Request._callback (/Users/Keith/Desktop/netPoetica/1- Projects/dcc-chargen/node_modules/nano/nano.js:296:11)
like image 427
netpoetica Avatar asked Dec 09 '12 08:12

netpoetica


3 Answers

The error was a missing bracket on an if statement in my EJS template. As that trace shows, it was in the exports.compile function (which is called by res.render()) that this error occurs) - the lib author uses a string to create a new function, which encloses my EJS file functionality in a try block, which becomes a dangling try because my missing opening bracket in my if block caused a syntax error in the new anonymous function created as a string and built with "new Function('str')".

@T.J.Crowder pointed out, that stack trace is perfectly clear, and did ultimately lead to this solution. I removed some of my example code because it definitely wasn't linked to the ultimate problem.

like image 132
netpoetica Avatar answered Nov 17 '22 22:11

netpoetica


For me it was a missing { on the below for loop caused the error. Look for proper closing or opening of paranthesis.

<% for(var i=0;i<users.length;i++) %>   
    <li> <%= users[i]   %>  </li>
    <%}%>
    </ul>
    </p>
like image 38
Jinesh Mathew Avatar answered Nov 17 '22 20:11

Jinesh Mathew


Some "hello world" examples of this error.

Scenario 1 (extra ,)

Extra , between try...catch (From the habit of "if-else" syntax).

/* Throw "message": "Uncaught SyntaxError: Missing catch or finally after try" */
try {
  /* do something */
},catch (error) {
  /* do something */
}

enter image description here

Valid code:

/* Valid code */
try {
 /* do something */
}
catch (error) {
 /* do something */
}

Scenario 2

Use try without catch -or- finally.

/* Throw error: Error: Missing catch or finally after try */
try {
  /* do something */
}

MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

like image 31
Ezra Siton Avatar answered Nov 17 '22 22:11

Ezra Siton