fix bug in handling exceptions