In this exercise, we will be practicing error handling in JavaScript by implementing selective catching, strict mode and try/catch/finally
blocks.
Exercise 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
Complete the following in your app.js
file
- Enable JavaScript's "Strict Mode" for the file
Complete 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
.
Complete 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
When you are finished, your console should resemble the following:
null
{ success: true }