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.
Which block should you use to handle errors that may occur during file reading in Deno?
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.
Which error type is typically thrown by Deno when attempting to open a non-existent file?
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.
How would you manually signal an error in Deno when a certain condition is not met?
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.
Which statement would help print values for debugging purposes in Deno?
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.
What information does a stack trace typically provide when an unhandled error occurs in Deno?
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.
Which method should you use to catch errors from a rejected Promise in Deno?
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.
Which statement can pause code execution at a specific line for debugging in Deno?
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.
Which property of the Error object contains the error message in Deno?
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.
What happens if an error is thrown in Deno and not caught by any try-catch block?
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.
For which purpose is the 'finally' clause usually used in a try-catch-finally statement in Deno?
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.