1. Catch exceptions
<1> Catch a single exception
# exception: An error encountered during the execution of the program code. The program will report an error and terminate the program code.
# Exception capture: it means that during the operation of the program code, when encountering an error, the program code is not terminated, and the program code is allowed to continue running, and a prompt message can be given to the user
# And record the error for later improvement
""" except the type of exception: the code in which the exception is executed """
print('Other codes...... ')
num = input('Enter a number :')
# ZeroDivisionError: division by zero
# ValueError: invalid literal for int() with base 10: 'a'
try:
num = 10 / int(num)
print('The calculated result is :', num)
except ZeroDivisionError:
print('You typed it wrong, please type it again')
print('Other codes...... ')
Copy the code
<2> Catch multiple exceptions
try:
print('-----test--1---')
open('123.txt'.'r') If the 123.txt file does not exist, IOError will be raised
print('-----test--2---')
print(num)# If the num variable is not defined, NameError is raised
except (IOError,NameError):
If you want to catch multiple exceptions with an except, you can use a tuple
Copy the code
Except (exception type 1, exception type 2,...) : code where exception execution occurred """
# print(' Other code...... ')
# num = input(' Please enter a number :')
# # ZeroDivisionError: division by zero
# # ValueError: invalid literal for int() with base 10: 'a'
# try:
# a = int(num)
# num = 10 / a
# print(' ', num)
# except (ZeroDivisionError, ValueError):
# print(' You typed wrong, please type again ')
#
# print(' Other code...... ')
""" except Exception type 1: exception 1, code executed except Exception type 2: exception 2, code executed except... : pass """
print('Other codes...... ')
num = input('Enter a number :')
# ZeroDivisionError: division by zero
# ValueError: invalid literal for int() with base 10: 'a'
try:
a = int(num)
num = 10 / a
print('The calculated result is :', num)
except ZeroDivisionError:
print('You typed it wrong, please type it again')
except ValueError:
print('Wrong input, please enter number')
print('Other codes...... ')
Copy the code
<3> Get the information description of the exception
Except (exception type 1, exception type 2,...) Print (variable name) """
print('Other codes...... ')
num = input('Enter a number :')
# ZeroDivisionError: division by zero
# ValueError: invalid literal for int() with base 10: 'a'
try:
a = int(num)
num = 10 / a
print('The calculated result is :', num)
except (ZeroDivisionError, ValueError) as e:
print('You typed it wrong, please type it again', e)
print('Other codes...... ')
Copy the code
<4> Catch all exceptions
try: Code in which an exception may occurexcept: The exception description cannot be obtainedThe code where the abnormal execution occurred ==============try: Code in which an exception may occurexcept Exception asE: code for abnormal executionprint(e)
pass
# Exception is the parent of a common Exception class,
ZeroDivisionError --> ArithmeticError --> Exception --> BaseException ---> object
ValueError --> Exception --> BaseException ---> object
Copy the code
print('Other codes...... ')
num = input('Enter a number :')
try:
a = int(num)
num = 10 / a
print('The calculated result is :', num)
f = open('1.txt'.'r')
except Exception as e:
print('You typed it wrong, please type it again', e)
print('Other codes...... ')
Copy the code
<4> Complete structure of the exception
try: Code in which an exception may occurexcept Exception asE: code for abnormal executionprint(e)
else: The code executes without exceptionfinally: Executes regardless of whether an exception occursCopy the code
print('Other codes...... ')
num = input('Enter a number :')
try:
a = int(num)
num = 10 / a
print('The calculated result is :', num)
except Exception as e:
print('You typed it wrong, please type it again', e)
else:
print('There is no exception, I will execute')
finally:
print('I'm going to execute whether there's an exception or not.')
print('Other codes...... ')
Copy the code