Many of the python's functions and methods produce exceptions and are flagged as errors or important events. An exception is also an object, and when converted to a string, the exception produces a message text. The simple syntax format for exception handling is as follows:
Try : try_suiteexcept exception1 as variable1: exception_suite1 ... except Exception2 as Variablen: exception_suiten
The exception handling logic is as follows:
If the suite in the try block works without generating an exception, the except module is skipped, and if an exception is created in the try block, the control flow immediately shifts to the first suite that matches the exception---this means that Statements in the suite following the statement that generated the exception are no longer executed, and if an exception occurs, and given the as variable part, inside the exception handling suite, Variale refers to the exception object. If an exception occurs when processing a except block, or if an exception cannot match any of the except blocks, Python searches for a matching except block within the next enclosing range. The search for the appropriate exception handling module is scaled out and can be extended to the call stack until a matching exception handling module is found, or a matching module cannot be found, in which case the program terminates with an unhandled exception, and Python prints the backtracking information along with the exception's message text. Description: The above content is excerpted from the Python3 Program Development Guide. Second edition of 1.2.5.4 Festival
Python Exception handling logic