1

When writing try/except statements, whether I use

except KeyError:

or

except KeyError as e:

I get the same result.

What is the difference between the two? Is KeyError as e just more specific/custom?

dillobird
  • 124
  • 13
user3915903
  • 1,185
  • 2
  • 9
  • 10

1 Answers1

3

When using except KeyError as e:, you can access the exception and its attributes as an object with e. Like so:

def test_function():
    try:
        do_something_that_fails()
    except Exception as e:
        print e.message, e.args

It will help with debugging any problems you have when the exception is thrown.

You can find more information on how this works in the Python Documentation.

Good luck!

Community
  • 1
  • 1
dillobird
  • 124
  • 13