๐ 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
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
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
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
Hint
Use console.log(result) to show the final output.
Practice
(1/5)
1.
What is the main purpose of using try and catch in JavaScript?
easy
A. To create new variables
B. To speed up the program execution
C. To handle errors and prevent the program from crashing
D. To repeat code multiple times
Solution
Step 1: Understand the role of try block
The try block runs code that might cause an error.
Step 2: Understand the role of catch block
The catch block handles any error from the try block to keep the program running smoothly.
Final Answer:
To handle errors and prevent the program from crashing -> Option C
Quick Check:
try-catch = error handling [OK]
Hint: Try-catch blocks catch errors to keep programs running [OK]
Common Mistakes:
Thinking try-catch speeds up code
Using try-catch to create variables
Confusing try-catch with loops
2.
Which of the following is the correct syntax to catch errors in JavaScript?
try { // code } catch (error) { // handle error }
easy
A. try { } catch (error) { }
B. try { } catch error() { }
C. try (error) { } catch { }
D. try { } catch error { }
Solution
Step 1: Check the try block syntax
The try block must be followed by catch with parentheses around the error variable.
Step 2: Verify the catch block syntax
The correct syntax is catch (error) { } with parentheses around the error parameter.
Final Answer:
try { } catch (error) { } -> Option A
Quick Check:
catch needs parentheses [OK]
Hint: Catch block always uses parentheses for error [OK]
Common Mistakes:
Omitting parentheses in catch
Using parentheses after try
Adding parentheses after catch keyword
3.
What will be the output of the following code?
try { let result = 10 / 0; console.log(result); } catch (e) { console.log('Error caught'); }
medium
A. Infinity
B. Error caught
C. 0
D. undefined
Solution
Step 1: Understand division by zero in JavaScript
Dividing 10 by 0 does not cause an error; it results in Infinity.
Step 2: Check if catch block runs
Since no error occurs, the catch block is skipped and Infinity is logged.
Final Answer:
Infinity -> Option A
Quick Check:
10/0 = Infinity, no error [OK]
Hint: Division by zero returns Infinity, no error thrown [OK]
Common Mistakes:
Assuming division by zero throws error
Expecting catch block to run
Confusing 0 with Infinity
4.
Find the error in this code and choose the correct fix:
try { console.log(x); } catch e { console.log('Error:', e.message); }
medium
A. Declare x before try block
B. Change catch e to catch (e)
C. Add finally block after catch
D. Remove the catch block
Solution
Step 1: Identify syntax error in catch block
The catch block must have parentheses around the error variable, so catch e is invalid syntax.
Step 2: Correct the catch syntax
Changing it to catch (e) fixes the syntax and allows error handling.
Final Answer:
Change catch e to catch (e) -> Option B
Quick Check:
Catch needs parentheses around error [OK]
Hint: Catch error variable must be in parentheses [OK]
Common Mistakes:
Omitting parentheses in catch
Adding finally when not needed
Ignoring undeclared variable error
5.
You want to safely parse JSON data that might be invalid. Which code snippet correctly catches parsing errors?