Modified PYTHON code to support exceptions.