Added a null check to prevent uncaught exceptions after caught exceptions.