Assess your understanding of advanced error handling concepts in JavaScript, including control flow with try/catch/finally, custom errors, stack traces, and the use of the throw statement.
Catching Specific Error Types
Given the code `try { JSON.parse(undefined); } catch(e) { if(e instanceof SyntaxError) console.log('Syntax'); }`, what will be logged to the console?
- Syntax
- Type
- Error
- Nothing
- Reference
Custom Error Throwing
Which is the correct way to throw an error with the message 'Invalid Data' in JavaScript?
- raise Error('Invalid Data');
- catch Error('Invalid Data');
- except('Invalid Data');
- throw Error('Invalid Data');
- throw.error('Invalid Data');
try/finally Control Flow
If a return statement is encountered in the try block, will the finally block still execute?
- Yes, finally always executes
- Only if catch is present
- Only if no error is thrown
- Only if return is conditional
- No, finally will be skipped
Implicit Error Propagation
What happens if an error is thrown inside the try block and there is no corresponding catch block?
- The error variable is set to undefined
- The script is silently ignored
- finally block prevents the error
- The error propagates up to the next higher scope
- A warning is logged but code continues
Error Object Properties
Which property of the Error object contains a description of the error?
- info
- detail
- code
- message
- desc
Stack Trace Access
After catching an error object 'e', what property can you access to retrieve the stack trace as a string?
- e.line
- e.cause
- e.tracing
- e.tracer
- e.stack
Rethrowing Errors
What is the effect of rethrowing an error from within a catch block using `throw e;`?
- It ignores the caught error
- It restarts the current block
- It propagates the error up to outer try/catch blocks
- It logs the error and terminates the program
- It creates a new error object
finally and Return
If both try and finally blocks contain return statements, which value will be returned from the function?
- The value from the try block
- The value from the finally block
- An error is thrown
- undefined
- The value from the catch block, if present
Error Subclassing
Which is the correct way to define a custom error class named 'ValidationError'?
- Error extends class ValidationError {}
- class ValidationError extends Error {}
- function ValidationError() extends Error {}
- class ValidationError implements Error {}
- object ValidationError inherits Error {}
throw Statement Restrictions
What types of values can you throw using the throw statement in JavaScript?
- Any JavaScript expression, including strings, numbers, or objects
- Primitive types only
- Only instances of Error and its subclasses
- Only error codes (numbers)
- Only string values
Erroneous try/catch Syntax
What is the result of attempting to use a try block without a corresponding catch or finally block?
- try acts as a new scope
- The try block defaults to a catch
- No error, but errors can’t be caught
- The code runs but try is ignored
- A syntax error occurs
Error Object Creation Behavior
What does `throw new Error('Oops');` actually do in JavaScript execution?
- Halts current execution and searches for catch/finally
- Resets all variables
- Ignores the error if uncaught
- Logs a warning but continues
- Only displays an alert
catch Parameter Scope
Where is the variable declared as the catch parameter accessible in JavaScript?
- Within try, catch, and finally
- Within the entire function
- Throughout the script
- Globally
- Only within the catch block
Misleading Catch Block Behaviors
Which of the following does NOT cause the catch block to execute?
- A manually thrown string in try
- A return statement in the try block
- A runtime exception in the try block
- A thrown error in the try block
- A thrown custom Error object
Inheritance of Error Properties
If you create `class CustomError extends Error {}` and throw an instance, which of these properties are inherited by default?
- signal and nameOnly
- reason and location
- cause and trace
- message and stack
- code and errno