1. What is the purpose of the 'catch' block in JavaScript?
A) To define code that runs if an error occurs
B) To handle exceptions thrown in the 'try' block
C) To define a function for error logging
D) To stop the execution of the program
Show Explanation
2. What is the function of the 'finally' block in a try-catch-finally statement?
A) To execute code regardless of an error
B) To handle the error
C) To log errors to the console
D) To define a block of code that may throw an error
Show Explanation
3. What does the 'throw' statement do in JavaScript?
A) It stops program execution
B) It ignores errors
C) It logs errors to the console
D) It creates a custom error
Show Explanation
4. What does the error object contain in a catch block?
A) Only the error message
B) The stack trace only
C) Information about the error
D) The line number where the error occurred
Show Explanation
5. How can you create a custom error in JavaScript?
A) By using 'new Error()'
B) By using 'throw'
C) By using 'catch'
D) By using 'try'
Show Explanation
6. Can a 'try' block throw multiple types of errors?
A) No, only one error can be thrown
B) Yes, but only specific errors
C) No, it can only throw string errors
D) Yes, it can throw any type of error
Show Explanation
7. Why is it important to use 'try-catch' in your code?
A) To prevent application crashes from runtime errors
B) To speed up the execution of the code
C) To enhance code readability
D) To automatically fix errors
Show Explanation
8. How can you create a custom error class in JavaScript?
A) By using 'Error.create()'
B) By using 'new CustomError()'
C) By extending the Error class
D) By using 'throw new Error()'
Show Explanation
9. What happens to the 'finally' block if no error occurs in the 'try' block?
A) It does not execute
B) It still executes
C) It executes only if an error occurs
D) It executes only if the 'catch' block executes
Show Explanation
10. What is the main benefit of using 'try-catch' for error handling?
A) It makes the code faster
B) It automatically fixes errors
C) It provides error logging
D) It allows graceful error handling
Show Explanation
11. What is a benefit of creating a custom error class in JavaScript?
A) It can add extra properties to the error
B) It makes the error handling process faster
C) It prevents errors from being thrown
D) It simplifies error messages
Show Explanation
12. When does the 'finally' block execute in a try-catch-finally statement?
A) Only if an error occurs
B) Only if no error occurs
C) After the 'try' and 'catch' blocks
D) Before the 'try' block
Show Explanation
13. What is the purpose of the 'throw' statement in JavaScript?
A) It suppresses errors
B) It creates a custom error message
C) It catches errors
D) It ignores exceptions
Show Explanation
14. How can custom error handling improve error management in applications?
A) It reduces the number of errors
B) It simplifies error messages
C) It automatically logs all errors
D) It allows defining actions for specific errors
Show Explanation
15. What is the primary purpose of the 'try' block in JavaScript?
A) To write code that may throw an error
B) To handle exceptions
C) To create custom errors
D) To log errors
Show Explanation
16. Why is it important to catch specific error types in JavaScript?
A) To avoid all errors
B) To allow precise error handling
C) To reduce error messages
D) To improve performance
Show Explanation
17. What type of code is typically placed inside a 'try' block?
A) Code that may throw an error
B) Code that runs successfully
C) Code that always throws an error
D) Code that requires no error handling
Show Explanation
18. How can you check if an error is of a specific type in JavaScript?
A) Using 'typeof'
B) Using 'error.name'
C) Using 'instanceof'
D) Using 'error.message'
Show Explanation
19. Why is error handling important in JavaScript applications?
A) To improve performance
B) To maintain stability and prevent crashes
C) To reduce error messages
D) To ensure faster execution
Show Explanation
20. What is the primary function of the 'catch' block in JavaScript?
A) To handle errors from the 'try' block
B) To execute code unconditionally
C) To define custom error messages
D) To ignore all errors
Show Explanation