Catching exceptions
Try: The code snippet below is the code snippet that needs to catch the exception;
Except: Catch the exception of a module, with the exception module name, with the reason parameter, except code for the exception occurs when the code executed, a try can correspond to multiple except clauses.
Note: Python 2 and 3 are handled differently on this side of the exception because the Python2 statement is:, followed by the reason variable name, and the Python3 statement is the exception module name as reason variable name
Other
Else: only after all except clauses, and only one, whose statement is the code that executes when no exception occurs for all except clauses (if there are other exceptions, an error is not performed)
Finally: Whether the exception occurs or does not occur, capturing or not capturing code snippets that are executed; Finally, you can define cleanup behavior in real-world scenarios where the finally clause is used to free external resources (such as file or network connections). Regardless of whether or not they were used in the process of error.
Example
Try: print (1/0) except Zerodivisionerror as err: # divisor is 0 error print (' Exception: ', err) except Arithmeticerror As err: # arithmetic error print (' Exception: ', err) except StandardError as err: # All built-in standard exception base class contains the above class print (' except Ion: ', err) except Exception as err: # General error base class print (' Exception: ', err) except baseexception as err: # All Exception base Class print (' Exception: ', err) Else: print ("No Errors") Finally: print ("No matter what, it'll always is her E. ")
Python in Try ... except