Implementing Exception Handling
Welcome to the "Implementing Exception Handling" Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Already have an account? Sign In »

Introduction
Welcome to the Implementing Exception Handling Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Exam Objectives
The following exam objective is covered in this lab:
- 1.4 Implement exception handling
Lab Duration
It will take approximately 20 minutes to complete this lab.
Exercise 1 - Handling Exceptions
Exception handling involves adding provisions to handle exceptional/abnormal conditions in a program. Such conditions, if not handled, forces the program to crash. You need to include exception-handling elements in the program to change the normal flow of program execution. A robust program will take care of all possible exceptions.
In JavaScript, it is quite common to have various types of errors in programs/scripts. You can handle such errors/exceptions using the try…catch block. The syntax of try…catch block is as follows:
try
{
}
catch(error)
{
}
You can use the try statement to define a code block that needs to be tested for any specific errors in the program. You can use the catch statement to define a code block that needs to be executed if an error is identified in the try code block. The try…catch block must be included in a pair. In other words, the try statement is not valid without a catch statement and vice-versa.
You can also include a finally statement after the try…catch block in the script. The syntax of the finally statement is as follows:
finally
{
}
The finally block is executed regardless of the outcome of the try…catch block. It does not matter to the compiler if the exceptions are handled or not. The compiler will go ahead and always execute the finally statement block.
In this exercise, you will learn to handle exceptions in JavaScript. You will perform the following tasks:
-
Task 1 - You will download the Visual Studio Code workspace (Calculator) from the Intranet and open the HTML page named Calc.html in the Calculator workspace.
-
Task 2 - You will learn to write JavaScript code to handle exceptions by using try…catch and finally statements.
Learning Outcomes
After completing this exercise, you will be able to:
- Handle exceptions in JavaScript
See the full benefits of our immersive learning experience with interactive courses and guided career paths.