Improve exception handling logic including error message