Original link: https://chegva.com/5471.html
◎Knowledge points
-
Add an else clause after the try-except statement
-
Add a finally clause after the try-except statement
-
Manually throwing exceptions using the raise statement
◎Script practice
▽ Add the else clause after the try-except statement
""" You can add an else clause after a while statement or a for-in statement, so that if the break statement in the loop body is not executed to exit the loop early, the else clause will be executed. Similarly, an else clause can be added after the try-except statement, and its syntax is: try: Code that may generate exception except exception class object 1: The current except clause handles the exception code except exception class object 2: The code that handles the exception in the current except clause... except exception class object n: The current except clause handles the exception code else: The code to be executed when no exception occurs in the try block""" try: # result = 1 / 0 result = 1 / 2 # result = int('abc') except ImportError: print("Import error") except ZeroDivisionError: print("0 cannot be used as a divisor") except TypeError: print("Type error") else: print(result) print("End") """ while True: try: x = int(input("Please enter an integer: ")) except ValueError: print("Invalid input, please input again") else: print("The input integer is: ", x) break """
[zkqw]
▽ Add a finally clause after the try-except statement
""" A finally clause can be added after a try-except statement, and its syntax is as follows: try: Code that may generate exception except exception class object 1: The current except clause handles the exception code except exception class object 2: The code that handles the exception in the current except clause... except exception class object n: The current except clause handles the exception code finally: code that will always be executed Because the finally clause is always executed, resources are usually released in the finally clause, such as: closing a file, closing a network connection, etc.""" try: result = 1 / 0 # result = 1 / 2 # result = int('abc') except ImportError: print("Import error") except ZeroDivisionError: print("0 cannot be used as a divisor") except TypeError: print("Type error") finally: print("Release resources") print("End")
""" You can add else clauses and finally clauses after the try-except statement. The syntax is as follows: try: Code that may generate exception except exception class object 1: The current except clause handles the exception code except exception class object 2: The code that handles the exception in the current except clause... except exception class object n: The current except clause handles the exception code else: The code to be executed when no exception is raised in the try block finally: code that will always be executed""" try: # result = 1 / 0 result = 1 / 2 # result = int('abc') except ImportError: print("Import error") except ZeroDivisionError: print("0 cannot be used as a divisor") except TypeError: print("Type error") else: print(result) finally: print("Release resources") print("End")
▽ Use the raise statement to manually throw exceptions
""" For the examples in the previous lessons, the exception instance is thrown automatically when an exception occurs. We can manually throw an exception instance object using the raise statement, which has the following syntax: raise exception class object [([parameter])] If no parameters are passed in, the parentheses """ can be omitted # raise ZeroDivisionError("0 cannot be used as a divisor") # raise ZeroDivisionError() # raise ZeroDivisionError try: raise ZeroDivisionError("0 cannot be used as a divisor") except ZeroDivisionError as err: print(err) """ If you don't want to handle the exception instance object in the except statement block, you can use the keyword raise to throw it as-is""" """ try: raise ZeroDivisionError("0 cannot be used as a divisor") except ZeroDivisionError: raise """ """ If you do not want to handle the exception instance object in the except statement block, you can also use the raise statement to manually throw another instance object of the exception class object""" try: raise ZeroDivisionError("0 cannot be used as a divisor") except ZeroDivisionError: raise ValueError("input error")
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/try_except_else_finally.py
[/zkqw]
This article is reprinted from: https://chegva.com/5471.html
This site is for inclusion only, and the copyright belongs to the original author.