throw new Error()
logs an error and stops the running file, right? I want to do something similar to throwing error, but I want the file to still keep running, like if you do normal console.log. How can I achieve that?
CodePudding user response:
You can use a try...catch
statement. If the code in the try block throws an exception then the code in the catch block will be executed. Then execution continues on afterwards.
let somethingElse = function() {
alert('something else')
}
try {
nonExistentFunction();
} catch (error) {
console.error(error);
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
somethingElse()
}
console.log('after')
CodePudding user response:
You have to wrap the entire main function inside a try catch block:
function main() { ... }
try {
main();
} catch (error) {
console.error('There was a critical error while running the app');
console.error(error);
}