1

I'm working on an existing function which is declared using use strict mode. What I found weird is that when an exception is thrown, the catch block is not executed. I think that it's related to the strict mode line because when I get ride of it, it goes in catch block.

I always saw the main goal of using try/catch is to prevent code to be exited suddenly (without any control) when there's uncontrolled situation at runtime.

A catch clause contains statements that specify what to do if an exception is thrown in the try block.

When used with strict mode, it seems like it doesn't have no effect. Is there any explanation of that behavior ?

My code:

function test(){
 "use strict"
 //...
 try {
  // var dt = new Date(2017, 10, 27, 15, 23, 9); /* works fine */
  var dt = new Date(2017, 10, 27, 15, 23, 09);
 } catch(e){
  console.log("inside catch");
 } finally {
    console.log("inside finally");
 }
 //...
}

test();
// output: Uncaught SyntaxError:
// Decimals with leading zeros are not allowed in strict mode.

Remark

Before asking the question, I looked at some questions at StackOverflow but I didn't found an explanation. For example this question is about the scope of const not the behavior of try/catch itself.

Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65
  • The use of octal is invalid syntax in strict mode, so the error is a Syntax Error. There is a previous discussion about this in https://stackoverflow.com/questions/5963045/syntax-errors-can-be-caught-in-javascript –  Nov 27 '17 at 11:34

0 Answers0