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
  • 17,051
  • 45
  • 159
  • 315

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,974
  • 7
  • 38
  • 69