1. What is the purpose of error handling in JavaScript?
Answer:
Explanation:
Error handling in JavaScript is used to manage exceptions and prevent the program from crashing due to errors. It ensures smoother execution by handling runtime errors.
2. How do you catch errors in JavaScript?
Answer:
Explanation:
The catch statement is used to catch and handle errors that occur in the try block. It specifies a block of code to execute in case an error occurs.
3. What keyword is used to throw custom errors in JavaScript?
Answer:
Explanation:
The throw keyword is used to throw custom errors. It allows the creation of custom error messages and conditions.
4. What is the correct syntax for a try-catch block in JavaScript?
Answer:
Explanation:
A try-catch block in JavaScript is written with the try keyword, followed by a block of code, and the catch keyword with an error object to handle any caught errors.
5. Can finally be used without catch in a try-catch block?
Answer:
Explanation:
The finally block can be used after a try block even without a catch block. It executes regardless of whether an error occurred.
6. What is the role of the finally block in error handling?
Answer:
Explanation:
The finally block is used to execute code after the try and catch blocks have completed, regardless of whether an exception was thrown or not.
7. How do you create a custom error in JavaScript?
Answer:
Explanation:
Custom errors can be created using the Error constructor, which allows for specifying an error message and, optionally, an error name.
8. What type of errors are caught by a try-catch block?
Answer:
Explanation:
A try-catch block in JavaScript is used to catch and handle runtime errors. Syntax errors are not caught since they occur during the parsing of the code.
9. Which method is used to log the stack trace of an error?
Answer:
Explanation:
The Error.stack property provides a stack trace that shows where the error occurred, which is useful for debugging.
10. What is a common practice for handling rejected promises?
Answer:
Explanation:
The .catch() method is commonly used to handle errors or rejections in promises. It provides a way to catch and handle any errors that occur during the execution of the promise.
11. What is a TypeError in JavaScript?
Answer:
Explanation:
A TypeError occurs in JavaScript when an operation is performed on a value of the wrong type, such as trying to access a property on undefined or null.
12. What is the difference between a ReferenceError and a SyntaxError?
Answer:
Explanation:
A ReferenceError in JavaScript is thrown when trying to access a variable that hasn't been declared, while a SyntaxError occurs when there are issues in the syntax of the code.
13. Can a catch block re-throw an error in JavaScript?
Answer:
Explanation:
It is possible for a catch block to re-throw an error using the throw keyword, which can be useful for propagating errors up to higher-level error handlers.
14. How can you handle multiple types of errors in a catch block?
Answer:
Explanation:
Within a catch block, you can differentiate between types of errors using if-else statements and the instanceof operator to handle different types of errors differently.
15. What is the use of the Error.name property in JavaScript?
Answer:
Explanation:
The Error.name property is used to specify the type or name of the error, such as TypeError, ReferenceError, or custom error names.