🚀 JavaScript Error Handling – 30 Practice Questions
🔹 Beginner Level (1–10)
1.
Write a program that divides two numbers using try...catch and handles division by zero.
2.
Create a try...catch block to handle an undefined variable error.
3.
Write code that parses invalid JSON and catches the error.
4.
Use finally to print “Execution Completed” whether error occurs or not.
5.
Create a function that throws an error if a number is negative.
6.
Handle an error when accessing a property of undefined.
7.
Write a program that catches invalid user input (non-number).
8.
Use try...catch to handle NaN results.
9.
Create a custom error using throw for empty string input.
10.
Write a program where catch prints only error.message.
🔹 Intermediate Level (11–20)
11.
Create a function that checks age and throws error if age < 18.
12.
Write a program to validate email format using throw.
13.
Handle JSON API response errors using try...catch.
14.
Create nested try...catch blocks.
15.
Write a function that throws different errors for:
- negative number
- zero
- valid number
16.
Use finally to close a simulated database connection.
17.
Handle array index out-of-bound error manually.
18.
Create a calculator that throws error for invalid operator.
19.
Write code that re-throws an error after catching it.
20.
Handle missing function arguments using throw.
🔹 Advanced Level (21–30)
21.
Create a custom error class using class extends Error.
22.
Handle asynchronous errors using try...catch with async/await.
23.
Write an API call using fetch and handle network errors.
24.
Create a retry mechanism if API fails (use error handling).
25.
Differentiate between SyntaxError, ReferenceError, and TypeError in code.
26.
Log errors to console and also store them in an array.
27.
Create a global error handler for a web app.
28.
Write a function that validates multiple fields and throws combined errors.
29.
Handle file upload errors (simulate using conditions).
30.
Build a form validator that:
- throws errors
- catches them
- displays user-friendly messages
💡 Bonus Tip (Important)
👉 In real projects:
- Use
try...catchfor runtime errors - Use
throw new Error()for custom validation - Use
finallyfor cleanup tasks






