Handle exceptions when processing documents