python

To share this page click on the buttons below;

Exceptions

To use code that can throw an exception:

try:
   # code to be executed that can throw exceptions
except:
   # code to be executed if some exception happens
else:
   # code to be executed it there are NO exceptions
finally:
   # code to be executed always (both in case of exception or not)

Catching an exception

With except: ALL exceptions are caught.

With except <exception> as ex: you catch a specific <exception>.

Create a new exception

Make a new class that inherits from the class Exception

class <MyException>(Exception):
   <code of the new exception class>

To share this page click on the buttons below;