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

--

--

--

Full Stack Web Developer | Javascript, React and Node Expert.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Avoid unsubscribing RXJS streams when using Angular

Hostile behind a good cause

Сutting-edge technologies in Planet Quest

To React Natively with Context or Not

Getting started with Node.js the right way!

Adding view count to your Nextjs Blog

Concepts of React — Part 3

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
Abdullah Alamin

Abdullah Alamin

Full Stack Web Developer | Javascript, React and Node Expert.

More from Medium

Simple JavaScript .fetch() explained

Why JavaScript Promises are awesome

ES6 Key Features Every JavaScript Developer Must Know

Arrow Functions in JavaScript, How is it different?