0

I know I can catch exceptions in python like:

try:
    with open('file.log') as file:
        read_data = file.read()
except:
    print('Exception!')

But how to get exception type or error message?

vico
  • 15,367
  • 39
  • 141
  • 262

1 Answers1

1
try:
    with open('file.log') as file:
        read_data = file.read()
except Exception as e:
    print(e)

You have to cast the Exception to a variable. This and more is in the Python documentation.

Josh Laird
  • 6,523
  • 6
  • 36
  • 65