Error Handling with try...catch block

Published on: April 22, 2017

Welcome to the 44th Easy JavaScript tutorial, part of We've looked at how to debug JavaScript in the past, let's go a little further by looking at try...catch. In the next tutorial, we'll cover throw().Try...catch is meant to allow you to try a block of code and define a response. This allows for cleaner and better error handling. By cleaner, I mean that you can actually control what error message your user sees using throw().

The try...catch block can throw 5 types of errors and two properties. The properties are always 'name' and 'message' - the errors are:


Error Name Description
RangeError A number "out of range" has occurred
ReferenceError An illegal reference has occurred
SyntaxError A syntax error has occurred
TypeError A type error has occurred
URIError An error in encodeURI() has occurred

Syntax of try...catch() block:

        //run code 
    } catch (error) {
        //Do something the code can't run or returns an error
        //the error parameter can be used to get the name and message //e.g. or error.message 
} finally {
//this code runs no matter what


To fork the fiddle and follow along:

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!