Javascript For Dummies: Try & Catch


//... execute your code here
} catch (error){
// what you want

‘Error’ object

try{variable; // undefined variable} catch(err){console.log(; // ReferenceErrorconsole.log(err.message); // variable is not defined}

Try … Catch … Finally

try{// ... your codeconsole.log('in the try block');} catch(err){console.log('in the catch block');} finally {console.log('I get executed in either state.');}

Throwing error

throw <error>
throw new Error('Your error message comes here');
new Error(message); // 
new SyntaxError(message); // = SyntaxError, err.message = message.
new ReferenceError(message);// = ReferenceError

Types of Errors



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store