Python catches exceptions (hm video lesson)

try:
    # Code to try to execute
    # Prompt the user to enter an integer
    num = int(input("Please enter an integer:"))
    # Divide the value 8 by the integer entered by the user and output
    result = 8 / num
    print(result)
except ValueError:
    # For error type 1, the corresponding code processing
    print("Please enter the correct integer")
except Exception as result:
    # Catch unknown errors and print error messages
    print("Unknown error %s" % result)
else:
    # Code that will be executed without error
    print("Attempt succeeded")
finally:
    # Code that will be executed regardless of whether there is an exception
    print("Code that will be executed regardless of whether there is an error")
print("-" * 50)

Guess you like

Origin blog.csdn.net/qq_34474071/article/details/123138232