0
0
Javascriptprogramming~15 mins

Catching runtime errors in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Catching runtime errors
πŸ“– Scenario: Imagine you are building a simple calculator app that divides two numbers. Sometimes users might enter zero as the divisor, which causes an error. You want to catch this error and show a friendly message instead of crashing.
🎯 Goal: You will create a program that safely divides two numbers and catches any runtime errors using try and catch. If an error happens, it will show a clear message.
πŸ“‹ What You'll Learn
Create two variables called numerator and denominator with exact values
Create a variable called result and set it to null
Use a try block to divide numerator by denominator and assign to result
Use a catch block to set result to the string 'Error: Cannot divide by zero'
Print the value of result
πŸ’‘ Why This Matters
🌍 Real World
Handling errors prevents apps from crashing and helps show friendly messages to users.
πŸ’Ό Career
Error handling is a key skill for developers to build reliable and user-friendly software.
Progress0 / 4 steps
1
Create the numbers to divide
Create a variable called numerator and set it to 10. Create another variable called denominator and set it to 0.
Javascript
Need a hint?

Use const to create the variables with the exact numbers.

2
Prepare a variable to hold the result
Create a variable called result and set it to null.
Javascript
Need a hint?

Use let because result will change later.

3
Use try and catch to divide safely
Write a try block where you divide numerator by denominator and assign it to result. Add a catch block that sets result to the string 'Error: Cannot divide by zero'.
Javascript
Need a hint?

Use throw new Error() inside try when denominator is zero. Catch the error and set result to the error message.

4
Show the result
Write console.log(result) to print the value of result.
Javascript
Need a hint?

Use console.log(result) to show the final output.