Added detection of caught exceptions so exit value is correct.