Skip to content
QuiznExam

Error Handling in JavaScript

Test your knowledge of error handling in JavaScript, including try, catch, finally, throw, built-in Error objects, custom errors, and handling asynchronous errors.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which statement is commonly used to handle exceptions in JavaScript?

    • A. try...catch
    • B. if...else only
    • C. switch...case
    • D. for...catch
    Show answer

    A. try...catch

    The try...catch statement allows JavaScript code to handle exceptions that occur during execution.

  2. 2. Which block contains code that may throw an exception?

    • A. catch
    • B. try
    • C. finally
    • D. throw
    Show answer

    B. try

    The try block contains code that is monitored for exceptions. If an exception occurs, control can move to the catch block.

  3. 3. What is the purpose of the catch block?

    • A. To handle an exception thrown from the try block
    • B. To execute code only when no error occurs
    • C. To create a loop
    • D. To declare a variable
    Show answer

    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.

  4. 4. What is the purpose of the finally block?

    • A. To execute cleanup code regardless of whether an exception occurs
    • B. To throw every error again
    • C. To prevent the catch block from executing
    • D. To define a new error type
    Show answer

    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.

  5. 5. Which keyword is used to explicitly throw an exception in JavaScript?

    • A. throw
    • B. error
    • C. raise
    • D. exception
    Show answer

    A. throw

    The throw statement allows JavaScript code to explicitly throw an exception.

  6. 6. Which is the recommended object to throw for a standard JavaScript error?

    • A. new Error()
    • B. new Exception()
    • C. new JavaError()
    • D. new RuntimeException()
    Show answer

    A. new Error()

    JavaScript provides the built-in Error object and specialized error types such as TypeError and RangeError.

  7. 7. What information is commonly available on an Error object?

    • A. message
    • B. errorTextOnly
    • C. exceptionCodeOnly
    • D. failureReasonOnly
    Show answer

    A. message

    An Error object commonly has a message property describing the error. Error objects may also provide a stack property.

  8. 8. What type of error can occur when an operation is performed on a value of an inappropriate type?

    • A. TypeError
    • B. SyntaxOnlyError
    • C. ValueException
    • D. CompileError
    Show answer

    A. TypeError

    A TypeError is thrown when an operation cannot be performed because a value is not of the expected type.

  9. 9. What happens when an exception is thrown inside a try block and a matching catch block is present?

    • A. Control transfers to the catch block
    • B. The program always terminates immediately
    • C. The finally block is skipped permanently
    • D. The exception is automatically ignored
    Show answer

    A. Control transfers to the catch block

    When an exception is thrown and a matching catch handler exists, control moves to that catch block.

  10. 10. Which syntax correctly combines try, catch, and finally?

    • A. try { } catch (error) { } finally { }
    • B. try ( ) catch { } final { }
    • C. try { } handle { } finallyError { }
    • D. catch { } try { } finallyError { }
    Show answer

    A. try { } catch (error) { } finally { }

    The standard syntax places a catch and optional finally block after the try block.

  11. 11. Can JavaScript throw values other than Error objects?

    • A. Yes
    • B. No
    • C. Only strings
    • D. Only numbers
    Show answer

    A. Yes

    JavaScript allows any value to be thrown, although throwing Error objects is generally preferred because they provide standard error information.

  12. 12. Which statement can be used to create a custom error type?

    • A. class MyError extends Error { }
    • B. error MyError { }
    • C. new ErrorType MyError { }
    • D. custom error MyError() { }
    Show answer

    A. class MyError extends Error { }

    A custom error class can extend the built-in Error class to provide application-specific error types.

  13. 13. What happens if an exception is not caught by a catch block in the current execution context?

    • A. It can propagate to an outer caller or execution context
    • B. It is always converted into a string
    • C. It is automatically converted into null
    • D. It is always ignored
    Show answer

    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.

  14. 14. How can errors from a rejected Promise commonly be handled?

    • A. Using catch() or try...catch with async/await
    • B. Using only finally()
    • C. Using switch()
    • D. Using setTimeout() only
    Show answer

    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.

  15. 15. What is a major benefit of proper error handling in JavaScript applications?

    • A. It allows applications to respond to failures gracefully and improve reliability
    • B. It guarantees that no errors will ever occur
    • C. It removes the need for testing
    • D. It makes all code synchronous
    Show answer

    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.