1、 Wrong type
ERROR abnormal : System type error , The cause may be a system crash , Out of memory , It is recommended that the procedure be terminated
Exception abnormal : Represents an exception that the program can handle , Can capture and possibly recover . This kind of problem can be solved .
2、 Error alert
Possible errors :
AttributeError .lOError . ImportError . IndexError、SyntaxError、TypeError、ValueError、KeyError、NameError.
Common mistakes are as follows :
. IndentationError: The indentation error
. KeyboardInterrupt: Ctrl+C Pressed
. UnboundLocalError : There is a global variable with the same name
3、 give an example
1、print(a)
NameError Wrong name , No such parameter
2、print(10/0)
ZeroDivisionError: division by zero The divisor cannot be zero 0
3、with open('hello.txt') as f:
pass
FileNotFoundError file error , There is no such document
try: Try to run the program
except:try Something went wrong with the running code ( There can be more than one )
else:try There is no problem running the program
finally: Code that will run in any case
give an example
execpt It is to select one from many
try: Try to run
a=1
print(b)
except NameError as name_error: If the name is wrong
print('name_error')
except KeyError as key_error: If the keyword is wrong
print('key_error')
except Exception as all_error: If there is a mistake, then
print('all_error')
finally: In the end, we will implement it uniformly
print(' end ')
1、raise Throw an exception
age = int(input('age:'))
if 0<age<150:
print(age)
else:
# Throw an exception
raise ValueError(" Age must be in 0~150 Between ")
# result
Input 100
ValueError: Age must be in 0~150 Between
2、 Custom exception
Through custom exception
# Custom exceptions
class AgeError(ValueError): Custom error module :AgeError, It belongs to ValueError class
pass
age = int(input('age:'))
if 0<age<150:
print(age)
else:
# Throw an exception
raise AgeError(" Age must be in 0~150 Between ")
# result
Input 1000
AgeError: Age must be in 0~150 Between
``
A new one was changed two days
效果:Automatically append permis