Python Tutorial, Release 3.9.5 (continued from previous page) ... <class 'Exception'> ('spam', 'eggs') ('spam', 'eggs') x = spam y = eggs If an exception has arguments, they are printed as the last part (‘detail’) of the message for unhandled exceptions. Exception handlers don’t just handle exceptions if they occur immediately in the try clause, but also if they occur inside functions that are called (even indirectly) in the try clause. For example: >>> def this_fails(): ... x =1/0 ... >>> try : ... this_fails() ... except ZeroDivisionError as err: ... print('Handling run-time error: ', err) ... Handling run-time error: division by zero 8.4 Raising Exceptions The$_{raise}$ statement allows the programmer to force a specified exception to occur. For example: >>> raise NameError('HiThere ') Traceback (most recent call last): File "<stdin>" , line1, in <module> NameError: HiThere The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception ). If an exception class is passed, it will be implicitly instantiated by calling its constructor with no arguments: raise ValueError # shorthand for 'raise ValueError()' If you need to determine whether an exception was raised but don’t intend to handle it, a simpler form of the$_{raise}$ statement allows you to re-raise the exception: >>> try : ... raise NameError('HiThere ') ... except NameError: ... print('An exception flew by! ') ... raise ... An exception flew by! Traceback (most recent call last): File "<stdin>" , line2, in <module> NameError: HiThere Chapter 8. Errors and Exceptions 60