Deno Error Handling and Debugging Essentials Quiz Quiz

Explore key principles of error handling and debugging in Deno, including proper use of try-catch, understanding error types, stack traces, and effective debugging practices. Strengthen your foundational skills for writing reliable and robust Deno scripts.

  1. Handling Exceptions

    Which block should you use to handle errors that may occur during file reading in Deno?

    1. catch-throw
    2. guard
    3. except-finally
    4. try-catch

    Explanation: The try-catch block is used to handle exceptions that may occur during runtime, such as file reading errors. Guard is not a valid block for error handling in this context, though it is common in other programming paradigms. Except-finally combines two different block types but is not correct in Deno or standard JavaScript. Catch-throw is incorrect because 'throw' is used to raise errors, not to handle them.

  2. Error Types

    Which error type is typically thrown by Deno when attempting to open a non-existent file?

    1. NotFound
    2. FileMissing
    3. NoFile
    4. FileError

    Explanation: The correct error type is 'NotFound', which Deno throws for operations where the specified file or directory does not exist. 'FileError' and 'FileMissing' are not standard error types in Deno. 'NoFile' sounds plausible but is not an actual error type.

  3. Throwing Custom Errors

    How would you manually signal an error in Deno when a certain condition is not met?

    1. stop('Condition failed')
    2. raise Error('Condition failed')
    3. fail Error('Condition failed')
    4. throw new Error('Condition failed')

    Explanation: The 'throw new Error(...)' syntax is used to manually raise an exception in Deno and similar environments. 'stop' and 'fail' are not valid JavaScript or Deno keywords. 'raise' is from other languages, but not usable here.

  4. Debugging with Console

    Which statement would help print values for debugging purposes in Deno?

    1. print(someValue)
    2. show(someValue)
    3. console.log(someValue)
    4. output(someValue)

    Explanation: The correct function is 'console.log', which outputs values to the terminal or console for debugging. 'print', 'output', and 'show' are not used for this purpose in Deno and will result in errors if used as shown.

  5. Understanding Stack Traces

    What information does a stack trace typically provide when an unhandled error occurs in Deno?

    1. Sequence of function calls leading to the error
    2. The full content of memory variables
    3. A history of user inputs
    4. A list of all executed modules

    Explanation: A stack trace provides the sequence of function calls that led to the error, which helps locate where the problem originated. It does not list all executed modules, show the exact values of all memory variables at the time of error, or provide user input history.

  6. Promise Error Handling

    Which method should you use to catch errors from a rejected Promise in Deno?

    1. .handle()
    2. .catch()
    3. .error()
    4. .catchError()

    Explanation: The '.catch()' method is used to handle errors from rejected Promises. '.handle()' and '.catchError()' look plausible but are not standard methods. '.error()' does not exist for Promises.

  7. Debugging Breakpoints

    Which statement can pause code execution at a specific line for debugging in Deno?

    1. stop;
    2. debugger;
    3. breakpoint;
    4. pause;

    Explanation: 'debugger;' is a statement that stops code execution when developer tools are open, letting you inspect the program state. 'pause;', 'breakpoint;', and 'stop;' are not valid JavaScript statements and will cause syntax errors.

  8. Error Object Properties

    Which property of the Error object contains the error message in Deno?

    1. message
    2. info
    3. reason
    4. description

    Explanation: The 'message' property holds the text describing the error. 'info', 'description', and 'reason' are not standard properties of the Error object, though they may be used as custom fields in some cases.

  9. Unhandled Exceptions

    What happens if an error is thrown in Deno and not caught by any try-catch block?

    1. The error is logged but execution continues
    2. The error is silently ignored
    3. The error is converted to a warning
    4. The program terminates and a stack trace is displayed

    Explanation: If no try-catch block handles the error, the program stops and prints a stack trace. Errors are not silently ignored or merely converted to warnings. Execution does not continue after an unhandled error.

  10. Finally Block Use Case

    For which purpose is the 'finally' clause usually used in a try-catch-finally statement in Deno?

    1. Repeating code until successful
    2. Executing cleanup code after try or catch
    3. Skipping code if no error occurs
    4. Logging only caught errors

    Explanation: The 'finally' block is designed for cleanup routines, like closing files or releasing resources, and runs whether or not an error occurs. It is not meant for just logging errors, skipping code, or looping operations.