python notes (week9) - exception

Python exception handling

Python provides two very important functions to handle exceptions and errors that occur during the execution of Python programs. You can use this function to debug python programs.

  • Exception handling: The Python tutorial on this site will introduce it in detail.
  • Assertions: The Python tutorial on this site will introduce them in detail.

python standard exception

exception name describe
BaseException Base class for all exceptions
SystemExit interpreter request to exit
KeyboardInterrupt User interrupts execution (usually by typing ^C)
Exception base class for general errors
StopIteration iterator has no more values
GeneratorExit Generator (generator) exception to notify exit
StandardError Base class for all built-in standard exceptions
ArithmeticError Base class for all numerical computation errors
FloatingPointError floating point calculation error
OverflowError Numerical operation exceeds maximum limit
ZeroDivisionError divide (or modulo) zero (all data types)
AssertionError Assertion statement failed
AttributeError Object does not have this property
EOFError No built-in input, reaching the EOF marker
EnvironmentError Base class for operating system errors
IOError I/O operation failed
OSError operating system error
WindowsError system call failed
ImportError Failed to import module/object
LookupError Base class for invalid data queries
IndexError There is no such index in the sequence (index)
KeyError There is no such key in the map
MemoryError Out of memory error (not fatal to the Python interpreter)
NameError undeclared/initialized object (no properties)
UnboundLocalError access uninitialized local variable
ReferenceError Weak reference attempts to access objects that have been garbage collected
RuntimeError General runtime errors
NotImplementedError Method not yet implemented
SyntaxError Python syntax error
IndentationError Indentation error
TabError Mixing Tabs and Spaces
SystemError General interpreter system errors
TypeError Invalid operation on type
ValueError Invalid parameter passed in
UnicodeError Unicode related errors
UnicodeDecodeError Error in Unicode decoding
UnicodeEncodeError Error in Unicode encoding
UnicodeTranslateError Error during Unicode conversion
Warning base class for warnings
DeprecationWarning Warning about deprecated features
FutureWarning Warning about future semantic changes of constructs
OverflowWarning Old warning about auto-promotion to long
PendingDeprecationWarning Warning that features will be deprecated
RuntimeWarning Suspicious runtime behavior warning
SyntaxWarning Suspicious syntax warning
UserWarning Warnings generated by user code

What is an exception?

An exception is an event that occurs during program execution and affects the normal execution of the program.

Typically, an exception occurs when Python cannot handle a program normally.

exceptions are Python objects representing an error.

When an exception occurs in the Python script, we need to catch it, otherwise the program will terminate execution.


try:
<语句>        #运行代码
except <名字> as e:
<语句>        #如果引发了'标准'异常,获得附加的数据e
except Exception as e:
<语句>        #如果引发了所有异常,获得附加的数据e
else:
<语句>        #如果没有异常发生
finally: 
<语句>        #都要执行

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325406395&siteId=291194637