Javascript Error Handling

1. What is the purpose of error handling in JavaScript?

a) To debug the code
b) To prevent the program from crashing
c) To identify syntax errors
d) To log errors to the console

Answer:

b) To prevent the program from crashing

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?

a) Using a for loop
b) Using the catch statement
c) Using a flag variable
d) Using the error event

Answer:

b) Using the catch statement

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?

a) error
b) throw
c) raise
d) emit

Answer:

b) throw

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?

a) try {…} catch(error) {…}
b) try {…} error {…}
c) try {…} catch {…}
d) try {…} exception {…}

Answer:

a) try {…} catch(error) {…}

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?

a) Yes
b) No
c) Only in strict mode
d) Only in non-strict mode

Answer:

a) Yes

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?

a) To log errors
b) To throw errors
c) To execute code after try and catch, regardless of the result
d) To handle only specific types of errors

Answer:

c) To execute code after try and catch, regardless of the result

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?

a) By using the Error constructor
b) By defining a new class
c) By using the customError function
d) By modifying the prototype of Error

Answer:

a) By using the Error constructor

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?

a) Only syntax errors
b) Only runtime errors
c) Both syntax and runtime errors
d) Only logic errors

Answer:

b) Only runtime errors

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?

a) console.trace()
b) console.error()
c) Error.stack
d) console.log()

Answer:

c) Error.stack

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?

a) Using a for loop
b) Using the reject() function
c) Using the .catch() method
d) Using the finally() method

Answer:

c) Using the .catch() method

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?

a) An error that occurs when a variable is not declared
b) An error related to incorrect use of types, such as accessing a property on undefined
c) An error that occurs when a function is not available
d) An error that occurs when a script takes too long to run

Answer:

b) An error related to incorrect use of types, such as accessing a property on undefined

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?

a) A ReferenceError occurs for undeclared variables, while a SyntaxError occurs for parsing errors
b) A ReferenceError is thrown for runtime issues, while a SyntaxError is thrown for logic errors
c) There is no difference
d) A ReferenceError is only thrown in strict mode

Answer:

a) A ReferenceError occurs for undeclared variables, while a SyntaxError occurs for parsing errors

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?

a) Yes, using the throw keyword
b) No, once caught, errors cannot be re-thrown
c) Only in strict mode
d) Only for certain types of errors

Answer:

a) Yes, using the throw keyword

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?

a) By using multiple catch blocks
b) By using if-else statements to differentiate errors
c) By using the instanceof operator
d) Both b and c

Answer:

d) Both b and c

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?

a) To specify the type of error
b) To log the error message
c) To determine the function where the error occurred
d) To indicate the line number of the error

Answer:

a) To specify the type of error

Explanation:

The Error.name property is used to specify the type or name of the error, such as TypeError, ReferenceError, or custom error names.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top