Fix inefficient exception handling