Bird
0
0

Given this code snippet, what will be the output when an uncaught exception occurs?

medium📝 component behavior Q4 of 15
Node.js - Error Handling Patterns
Given this code snippet, what will be the output when an uncaught exception occurs? const http = require('http'); const server = http.createServer((req, res) => { throw new Error('Oops'); }); process.on('uncaughtException', (err) => { console.log('Error caught:', err.message); process.exit(1); }); server.listen(3000); // A request is made to the server
ALogs 'Error caught: Oops' but server keeps running
BServer crashes without logging anything
CLogs 'Error caught: Oops' and exits with code 1
DNo output, server ignores the error
Step-by-Step Solution
Solution:
  1. Step 1: Understand uncaughtException handler

    The handler logs the error message and calls process.exit(1) to stop the app.
  2. Step 2: Analyze behavior on thrown error

    The thrown error triggers the handler, so it logs and exits with code 1.
  3. Final Answer:

    Logs 'Error caught: Oops' and exits with code 1 -> Option C
  4. Quick Check:

    Uncaught exception logs and exits [OK]
Quick Trick: Uncaught exceptions trigger handler then exit if called [OK]
Common Mistakes:
  • Thinking server keeps running after exit
  • Expecting no logs
  • Confusing error handling with ignoring errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes