Error Handling

Exceptions

Raising them

You can raise exceptions with raise keyword.

raise Exception("Some exception")

raise TypeError("Only integers")

And many other types.

Catching them

Catch them with try and have multiple except statements for different exceptions.

try:
    ...
except Exception as e:
    print(repr(e))
except:
    ...
else:
    ...
finally:
    ...

The else block executes code when there is no error.

The finally block executes code regardless if there were errors or not.

Last updated