More pythonic way of handling exceptions