Exercise: JS Error Handling
#
ObjectiveIn this exercise, we will be practicing error handling in JavaScript by implementing selective catching, strict mode and try/catch/finally
blocks.
#
Steps#
Getting StartedExercise Repo: JS Error Handling
- Open your command line and navigate to your repos directory (if you do not have a repos folder, then you can use mkdir repos to create one)
- Use this template repository to start a new project in your repos folder:
git clone <repo_link>
- cd repo_name to navigate into your new repo directory
- Start Visual Studio Code and select 'Open Folder'. Then select repo_name to open the folder in the editor (or just type code . in your terminal inside the repo directory)
- Follow the instructions on the README.md file to complete exercises
- Open the app.js file to get started
#
Exercise 1: Enable Strict ModeComplete the following in your app.js
file
- Enable JavaScript's "Strict Mode" for the file
#
Exercise 2: Function Syntax DebuggingComplete the following in your app.js
file
- There is a problem with the function syntax. Fix the issue before you finish.
HINT: The issue will cause
data
to be read asnull
.
#
Exercise 3: Using Try/CatchComplete the following in your app.js
file
- Add a try/catch block in the function body
- Try to return the data parsed to JSON
- Catch any raised exceptions
- If an exception is caught:
- Print the error to the console
- Return null
#
On CompleteWhen you are finished, your console should resemble the following: