Node.js Error Handling and Debugging Fundamentals Quiz Quiz

Dive into essential Node.js concepts with this quiz focused on error handling and debugging. Enhance your understanding of callbacks, asynchronous errors, exception handling, and best debugging practices used in Node.js development.

  1. Error Handling in Callbacks

    What is the typical convention for handling errors in Node.js callback functions?

    1. Returning the error from the callback
    2. Passing the error as the first argument
    3. Throwing the error outside the callback
    4. Adding an error handler as the last argument

    Explanation: Node.js callbacks usually follow the 'error-first' pattern, where the error is passed as the first argument to the callback. This lets the function receiving the callback easily check and handle errors. Adding an error handler as the last argument or returning the error from the callback does not follow the standard pattern. Throwing the error outside the callback fails to provide proper error context within asynchronous operations.

  2. Try-Catch with Async Code

    Which scenario will try-catch blocks NOT handle errors in Node.js?

    1. Errors thrown inside synchronous code
    2. Errors thrown inside asynchronous callbacks
    3. Syntax errors during script startup
    4. Errors thrown in a try block before an asynchronous operation

    Explanation: Try-catch blocks only catch errors that occur synchronously within their scope. Errors inside asynchronous callbacks will be missed because the callback runs after the current tick. Synchronous mistakes or syntax issues can be caught by try-catch (or will halt execution), but errors thrown inside asynchronous callbacks are not caught due to the event loop. Placing a try-catch around asynchronous code does not guarantee error capture for non-sync issues.

  3. Handling Uncaught Exceptions

    Which Node.js process event can be used to handle uncaught exceptions globally?

    1. errorCaught
    2. catchAllErrors
    3. uncaughtException
    4. exceptionEvent

    Explanation: Node.js emits an 'uncaughtException' event on the process object when exceptions bubble up unhandled. This provides a last-resort error handler for uncaught issues. 'errorCaught', 'catchAllErrors', and 'exceptionEvent' are not defined Node.js events and will not respond to global exceptions.

  4. Promise Error Handling

    How should you handle errors that occur inside a Promise in Node.js?

    1. By wrapping the code in a try-catch block
    2. By using the .catch() method
    3. By placing a callback after the promise
    4. By ignoring the error

    Explanation: The .catch() method is specifically designed to handle errors or rejected promises in asynchronous Promise chains. Wrapping Promise code in a try-catch block does not catch asynchronous errors inside the promise. Ignoring errors or placing callbacks after the promise will not ensure proper error handling. Using .catch() guarantees that exceptions thrown inside a promise are managed.

  5. Asynchronous Debugging Tool

    Which method is commonly used to output debug information to the console in Node.js?

    1. console.log
    2. print.output
    3. debug.write
    4. system.echo

    Explanation: console.log is a built-in Node.js method used to output information for debugging purposes. The other options (system.echo, print.output, and debug.write) are not standard Node.js functions and will result in errors if used. console.log provides a simple and effective way to view variable data, error messages, and program flow.

  6. Event Emitter Error Events

    What happens if an 'error' event is emitted from an EventEmitter but no handler for 'error' is attached?

    1. Node.js will log a warning but continue running
    2. The event will be silently ignored
    3. The process will crash with an uncaught exception
    4. The event will be automatically retried

    Explanation: Emitting an 'error' event without any listeners attached triggers an uncaught exception, causing the process to crash. The event is not ignored, no warning is logged without a crash, and Node.js does not retry the event. Attaching appropriate handlers for 'error' events is important to prevent unexpected terminations.

  7. Async/Await Error Pattern

    What is the best practice for handling errors in async functions using async/await in Node.js?

    1. Only use .catch() after calling the function
    2. Wrap await statements in try-catch blocks
    3. Ignore errors and let them propagate
    4. Use callback functions inside each async function

    Explanation: Using try-catch blocks around await statements allows developers to handle errors gracefully within async functions. Relying solely on .catch() works for Promises but does not directly handle errors in async functions without a wrapper. Using callbacks inside async functions is not recommended since async/await is designed to avoid callback patterns. Ignoring errors can lead to unhandled promise rejections.

  8. Identifying Syntax Errors

    When starting a Node.js script with a misplaced bracket in the code, what kind of error will be thrown?

    1. ReferenceError
    2. SyntaxError
    3. TypeError
    4. FormatError

    Explanation: A misplaced bracket is a structural mistake and results in a SyntaxError, meaning the script cannot be parsed. TypeError occurs for invalid operations on types, ReferenceError for using variables that do not exist, and FormatError is not a standard JavaScript error type. Syntax errors prevent the script from execution entirely.

  9. Uncaught Promise Rejection

    What is the effect of an unhandled rejected promise in modern versions of Node.js?

    1. It will be ignored silently
    2. It will retry the promise after some time
    3. It will always crash the operating system
    4. It will raise a warning and may terminate the process

    Explanation: Modern Node.js raises a warning for unhandled promise rejections and may terminate the process if they are not handled. They are not ignored silently. The script does not crash the entire operating system, and no automatic retry occurs. It is good practice to catch and handle all promise rejections to prevent unwanted program termination.

  10. Detecting Memory Leaks

    Which symptom is commonly observed in a Node.js application with a memory leak?

    1. Frequent network disconnections
    2. Gradually increasing memory usage over time
    3. Sudden process termination on startup
    4. All console output stops immediately

    Explanation: Memory leaks lead to a gradual, persistent rise in memory consumption, indicating retained unreachable objects. Sudden process termination may result from other fatal errors, not specifically leaks. Stopping all console output and frequent network disconnections are unrelated side effects and are not definitive indicators of memory leaks in a typical application.