nodejs catch uncaught exception

// Step 1: Import the 'events' module
const events = require('events');

// Step 2: Create an EventEmitter instance
const eventEmitter = new events.EventEmitter();

// Step 3: Define a function to handle uncaught exceptions
const handleUncaughtException = (err) => {
  console.error('Uncaught Exception:', err);
  // Perform any necessary cleanup or logging here
  process.exit(1); // Exit the process with a non-zero status code
};

// Step 4: Attach the 'uncaughtException' event listener
eventEmitter.on('uncaughtException', handleUncaughtException);

// Step 5: Set the global exception handler to the EventEmitter
process.on('uncaughtException', (err) => {
  eventEmitter.emit('uncaughtException', err);
});

// Step 6: Simulate an uncaught exception
setTimeout(() => {
  throw new Error('Simulated Uncaught Exception');
}, 1000);