Handle uncaught exceptions more gracefully