Search notes:

Python: try

Demo

for i in range (-7, 7):

    try:
       print(f'1000 / {i} = {1000/i}')

    except ZeroDivisionError:
       print("i is zero, division impossible")
Github repository about-python, path: /statements/try/demo.py

User defined exceptions

By deriving a class from an exception class (such as BaseException), it is possible to create a user defined exception that can be thrown and caught:
class SomethingIsWrong(BaseException):

      def __init__(self, reason):
          self.reason = reason

      def __str__(self):
          return self.reason


try:
   print('one')
   raise SomethingIsWrong('xyz')
   print('two')

except SomethingIsWrong as somethingWrong:
    print(f'Exception caught: {str(somethingWrong)}')

Github repository about-Python, path: /statements/try/user-defined-exception.py
Note that Python's documentation recommends to derive from Exception rather than from BaseException.

See also

The else clause
exception handling

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...', 1758199199, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/statements/try/index(76): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78