Handle exceptions and output error