Search notes:

Python: Exception handling

class  Ex_Base(BaseException):
       pass

class  Ex_Deriv(Ex_Base):
       pass

#      --------------------------------------

try:
       print('Before raising the exception')
       raise Ex_Deriv()
       print('After raising the exception')

except Ex_Deriv as e:               # Order of except statements is important.
       print('Ex_Deriv caught')     # Try to catch the more concrete class first.

except Ex_Base as e:
       print('Ex_Base caught')


#      --------------------------------------

try:
       print('Before raising the exception')
       raise Ex_Deriv()
       print('After raising the exception')

except Ex_Base as e:                # Probably wrong order.
       print('Ex_Base caught')

except Ex_Deriv as e:
       print('Ex_Deriv caught')
Github repository about-Python, path: /exception-handling/demo.py

BaseException

BaseException is the base class from which all exceptions must be derived.

See also

The try, except and raise statements.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758192377, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/exception-handling/index(74): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78