JavaScript Error Handling - Try/catch/finally statement

In this lesson we are going to be talking about how to handle errors in JavaScript using the try/catch/finally statement.

Desired Outcomes:

  • Understand how to catch errors in JavaScript
  • Learn how to handle errors in JavaScript

The try/catch/finally statement is used to handle errors or exceptions. An exception is an object generated by the JavaScript interpreter that passes the information that something unexpected has happened in the program. Also, the programmer can declare some operation as unexpected and throw an error using the throw statement.

The syntax of the command is as follows:

try {
   // code that is trying to execute
}
catch(exception) {
   // code that will execute if the code in try cannot
}
finally {
   // code will always execute whether there is an exception or not
}

In the following example, the user is asked to enter a number in the dialog box. If he does not enter a number, an exception will be returned.

<script>
   var number = prompt("Enter a number: ");
   try {
      if (isNaN(number)) {
         throw "The entered value is not a number!";
      } else {
         alert("The entered number is: " + number);
      }
   } catch (exception) {
    alert("The following error occurred when entering the number: " + exception);
   } finally {
    alert("Thank you for participating!");
   }
</script>

If a number is entered in the prompt dialog box, that number is displayed using the alert() command. If any character other than a number is entered, an exception will be displayed. After the given input, a thank you message is displayed.

In the previous example, the programmer returns an exception with the throw command, and for all other exceptions, the Error object is used, which contains two properties: name and message.

<script>
   var number = prompt("Enter a number: ");
   try {
     alert(undeclaredVariable);
   } catch (ex) {
     alert("An error occurred: name = " + ex.name + ", message= " + ex.message );
   } finally {
     alert("Finally block");
   }
</script>

In the above example, the message in the catch block will contain the message: "An error occurred: name=ReferenceError, message = undeclaredVariable is not defined".

<script>
   var number = prompt("Enter a number: ");
   try {
      if (isNaN(number)) {
         throw "The entered value is not a number!";
      } else {
         alert("The entered number is: " + number);
      }
   } catch (exception) {
    alert("The following error occurred when entering the number: " + exception);
   } finally {
    alert("Thank you for participating!");
   }
</script>

If a number is entered in the prompt dialog box, that number is displayed using the alert() command. If any character other than a number is entered, an exception will be displayed. After the given input, a thank you message is displayed.

In the previous example, the programmer returns an exception with the throw command, and for all other exceptions, the Error object is used, which contains two properties: name and message.

<script>
   var number = prompt("Enter a number: ");
   try {
      alert(undeclaredVariable);
   } catch (ex) {
      alert("An error occurred: name = " + ex.name + ", message= " + ex.message );
   } finally {
   }
</script>

In the above example, the message in the catch block will contain the message: "An error occurred: name=ReferenceError, message = undeclaredVariable is not defined".

Updated on:

Part of our complete: JavaScript Fundamentals guide

Want exercises and real projects for this lesson? Get the full course.