Javascript For Dummies: Try & Catch

Syntax

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

‘Error’ object

try{variable; // undefined variable} catch(err){console.log(err.name); // 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); // err.name = SyntaxError, err.message = message.
new ReferenceError(message);// err.name = 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