

If an exception occurs during execution of the tryĬlause, the exception may be handled by an exceptĬlause. Points discuss more complex cases when an exception occurs: Not the try statement produces an exception. If a finally clause is present, the finallyĬlause will execute as the last task before the try Goodbye, world! KeyboardInterrupt Traceback (most recent call last): The try statement has another optional clause which is intended toĭefine clean-up actions that must be executed under all circumstances. Many standard modules define their own exceptions to report errors that may Most exceptions are defined with names that end in “Error”, similar to the Information about the error to be extracted by handlers for the exception. Exceptions should typicallyīe derived from the Exception class, either directly or indirectly.Įxception classes can be defined which do anything any other class can do, butĪre usually kept simple, often only offering a number of attributes that allow Programs may name their own exceptions by creating a new exception class (seeĬlasses for more about Python classes). Traceback (most recent call last):įor more information about chaining mechanics, see Built-in Exceptions. The exception and then re-raise it (allowing a caller to handle the The most common pattern for handling Exception is to print or log Of exceptions that we intend to handle, and to allow any unexpected

However, it is good practice to be as specific as possible with the types KeyboardInterrupt which is raised when a user wishes to interruptĮxception can be used as a wildcard that catches (almost) everything. They include SystemExit which is raised by sys.exit() and Handled, because they are used to indicate that the program should terminate. Subclasses, Exception, is the base class of all the non-fatal exceptions.Įxceptions which are not subclasses of Exception are not typically The exception’s _str_() output is printed as the last part (‘detail’)īaseException is the common base class of all exceptions. ('spam', 'eggs') ('spam', 'eggs') x = spam y = eggs # but may be overridden in exception subclasses. print ( inst ) # _str_ allows args to be printed directly. print ( type ( inst )) # the exception instance. May name multiple exceptions as a parenthesized tuple, for example: Not in other handlers of the same try statement. Handlers only handle exceptions that occur in the corresponding try clause, If an exception occurs which does not match the exception named in the exceptĬlause, it is passed on to outer try statements if no handler isįound, it is an unhandled exception and execution stops with a message asĪ try statement may have more than one except clause, to specify Then, if its type matches the exception named after theĮxcept keyword, the except clause is executed, and then execution

If an exception occurs during execution of the try clause, the rest of theĬlause is skipped. If no exception occurs, the except clause is skipped and execution of the įirst, the try clause (the statement(s) between the try and x = int ( input ( "Please enter a number: " )). Is signalled by raising the KeyboardInterrupt exception. Whatever the operating system supports) note that a user-generated interruption Look at theįollowing example, which asks the user for input until a valid integer has beenĮntered, but allows the user to interrupt the program (using Control- C or It is possible to write programs that handle selected exceptions. Traceback listing source lines however, it will not display lines read fromīuilt-in Exceptions lists the built-in exceptions and their meanings. Occurred, in the form of a stack traceback. The preceding part of the error message shows the context where the exception The rest of the line provides detail based on the type of exception and what

StandardĮxception names are built-in identifiers (not reserved keywords). This is true for all built-in exceptions, but need not be trueįor user-defined exceptions (although it is a useful convention). The string printed as the exception type is the name of the built-in exception The example are ZeroDivisionError, NameError and TypeError. Exceptions come inĭifferent types, and the type is printed as part of the message: the types in The last line of the error message indicates what happened. 10 * ( 1 / 0 ) Traceback (most recent call last):įile "", line 1, in ZeroDivisionError: division by zero > 4 + spam * 3 Traceback (most recent call last):įile "", line 1, in NameError: name 'spam' is not defined > '2' + 2 Traceback (most recent call last):įile "", line 1, in TypeError: can only concatenate str (not "int") to str
