Improved exception logging and handling