DOM Manipulation in JavaScript
15 questions · 12 min · Medium
Test your knowledge of error handling in JavaScript, including try, catch, finally, throw, built-in Error objects, custom errors, and handling asynchronous errors.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. try...catch
The try...catch statement allows JavaScript code to handle exceptions that occur during execution.
B. try
The try block contains code that is monitored for exceptions. If an exception occurs, control can move to the catch block.
A. To handle an exception thrown from the try block
The catch block receives control when an exception is thrown from the associated try block.
A. To execute cleanup code regardless of whether an exception occurs
The finally block is commonly used for cleanup operations and normally executes whether an exception is thrown or not.
A. throw
The throw statement allows JavaScript code to explicitly throw an exception.
A. new Error()
JavaScript provides the built-in Error object and specialized error types such as TypeError and RangeError.
A. message
An Error object commonly has a message property describing the error. Error objects may also provide a stack property.
A. TypeError
A TypeError is thrown when an operation cannot be performed because a value is not of the expected type.
A. Control transfers to the catch block
When an exception is thrown and a matching catch handler exists, control moves to that catch block.
A. try { } catch (error) { } finally { }
The standard syntax places a catch and optional finally block after the try block.
A. Yes
JavaScript allows any value to be thrown, although throwing Error objects is generally preferred because they provide standard error information.
A. class MyError extends Error { }
A custom error class can extend the built-in Error class to provide application-specific error types.
A. It can propagate to an outer caller or execution context
If an exception is not handled at the current level, it can propagate up the call stack until a handler is found or the exception remains uncaught.
A. Using catch() or try...catch with async/await
Promise rejections can be handled with promise.catch(), while async functions can use try...catch around awaited operations.
A. It allows applications to respond to failures gracefully and improve reliability
Proper error handling allows applications to detect failures, perform appropriate recovery or cleanup, and provide useful feedback instead of failing unexpectedly.