Reputation: 7891
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.
Upvotes: 1
Views: 402
Reputation: 916
Here is the key: octal.
Yes, I agree with you, it is weird. But… this is what it is. Let's see:
The reason is that in the past (I think, as early as in ECMAScript 3), the leading zero was interpreted as a prefix to indicate an octal number. Later on, to avoid confusion, they decided to ban this form of literal in strict mode and show the syntax error you observed.
The modern octal literal syntax is using the Latin letter ‘o’ (‘O’), so the prefix is ‘0o’ or ‘0O’. Please see, for example, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal_literal.
Upvotes: 0