I learnt about hoisting in JavaScript , and wanted to experiment the kind of errors i could get , and to catch them. However i can't figure out how to catch this precise error:
SyntaxError : Missing initializer in const declaration.
Here is the piece of code:
try {
g = 'Hello !';
console.log(g);
const g;
} catch(err) {
console.log('You got an error '+err);
}
console.log('rest of the code.')
I tested this try-catch with other exceptions, and everything seemed to work correctly. So I thought the problem was about hoisting and maybe variables were read right at the start of the block hence were not caught.
I tried to encapsulate the whole code in a try-catch but without any conclusive results. Here is my code encapsulated:
function hoisting() {
try {
g = 'Hello !';
console.log(g);
const g;
} catch(err) {
console.log('You got an error '+err);
}
console.log('rest of the code.');
}
function main() {
try {
hoisting();
} catch(err) {
console.log(err);
}
}
main();