Chapter 8. Errors and Exceptions 62 Python Tutorial, Release 3.9.5 (continued from previous page) Most exceptions are defined with names that end in “Error”, similar to the naming of the standard exceptions. Many standard modules define their own exceptions to report errors that may occur in functions they define. More information on classes is presented in chapter Classes . The$_{try}$ statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances. For example: If a$_{finally}$ clause is present, the$_{finally}$ clause will execute as the last task before the$_{try}$statement completes. The$_{finally}$clause runs whether or not the$_{try}$ statement produces an exception. The following points discuss more complex cases when an exception occurs: • If the$_{try}$ statement reaches a$_{break}$,$_{continue}$or$_{return}$statement, the$_{finally}$ clause will execute just prior to the$_{break}$,$_{continue}$or$_{return}$statement’s execution. • If the$_{finally}$ clause executes a$_{break}$,$_{continue}$or$_{return}$statement, exceptions are not re-raised. • An exception could occur during execution of an$_{except}$or$_{else}$clause. Again, the exception is re-raised after the$_{finally}$ clause has been executed. • If an exception occurs during execution of the $_{try}$clause, the exception may be handled by an except clause. If the exception is not handled by an$_{except}$clause, the exception is re-raised after the$_{finally}$ clause has been executed. >>> try : ... raise KeyboardInterrupt ... finally : ... print('Goodbye, world! ') ... Goodbye, world! KeyboardInterrupt Traceback (most recent call last): File "<stdin>" , line2, in <module> 8.7 Defining Clean-up Actions Attributes: expression -- input expression in which the error occurred message -- explanation of the error """ def __init__ (self, expression, message): self.expression =expression self.message =message classTransitionError (Error): """Raised when an operation attempts a state transition that's not allowed. Attributes: previous -- state at beginning of transition next -- attempted new state message -- explanation of why the specific transition is not allowed """ def __init__ (self, previous,next, message): self.previous =previous self.next =next self.message =message