Errors — Throwing, Catching and Custom Types — Concept Check
Questions on Errors — Throwing, Catching and Custom Types. Throw Error objects, not strings. Catch where you can act, not everywhere. And know the two ways an async error escapes a try/catch.
Questions in this quiz
8 questions on Errors — Throwing, Catching and Custom Types — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a function that fetches user data asynchronously. What happens if you forget to use 'await' inside a try/catch block?
- AThe function returns undefined.
- BThe error becomes an unhandled rejection.
- CThe function throws a synchronous error.
- DThe error is caught by the catch block.
2.In JavaScript, why should you throw Error objects instead of strings?
- AThrowing strings is faster but less informative.
- BError objects preserve the stack trace.
- CError objects are required by the JavaScript standard.
- DStrings cannot be caught in a try/catch block.
3.A developer uses setTimeout to execute a function that throws an error. Why does the surrounding try/catch block fail to catch it?
- AThe try/catch block is incorrectly implemented.
- BThe error is asynchronous and escapes the stack.
- CsetTimeout does not support error handling.
- DThe error is caught but not logged.
4.You have a custom error class 'ApiError' with a 'status' property. How can you handle a 401 status differently?
- ACatch the error and rethrow with a different message.
- BCheck if the error is an instance of ApiError and status is 401.
- CCheck the error message for '401'.
- DUse a switch statement on the error object.
5.What is a potential risk of catching errors at a low level in your code?
- AIt may hide errors and propagate bad state.
- BIt prevents any unhandled rejections.
- CIt ensures all errors are logged properly.
- DIt simplifies debugging by localizing errors.
6.Why might a developer choose to use a custom error class like 'ValidationError'?
- ATo distinguish error types without parsing messages.
- BTo ensure errors are always caught.
- CTo improve error performance.
- DTo reduce the size of error objects.
7.A function retries an API call up to 3 times. Why might it throw an error immediately on a 4xx status?
- A4xx errors are client-side and unlikely to succeed on retry.
- B4xx errors indicate server overload.
- C4xx errors are automatically handled by the browser.
- D4xx errors are usually temporary and should be retried.
8.What happens if you rethrow an error with a new 'ApiError' while preserving the original error?
- AThe original error is logged automatically.
- BThe original error is lost.
- CThe new error overwrites the original error.
- DThe original error is accessible via the 'cause' property.