To try… Except… The else… finally
def self_error(type_list) :
The exception handler passes an iterable and handles the error case of passing parameters: paramType_list: an iterable """
try:
for i in type_list:
print(i)
except:
print("Not an iterable")
else:
print("No error occurred.")
finally:
print("Program completed")
self_error("abc")
"" no error occurs in a, B, and C. The program is finished. ""
self_error(2)
"" not an iterable program ends running """
Copy the code
It can be seen from the program that if the try part is executed successfully, the else part is executed, and if the try part is in error, the except part is executed, but in both cases the finally part is printed. Of course, the else and finally parts can be omitted. We can customize the output error type by using the raise function.
#Python Learning exchange QQ group: 531509025
def self_error(type_num) :
""" Exception handler Function A user-defined exception handler used to throw an exception :paramtype_num: Value used to check whether an exception is thrown :return Exception information """
if type_num<10:
raise ValueError("Value less than 10")
else:return 200
if __name__=="__main__":
self_error(11) # 200
self_error(9) #ValueError: The value is less than 10
Copy the code
Raise raises exceptions that only apply to Python’s standard exception classes
Unusual name | describe |
---|---|
ArithmeticError | Base class for all numeric errors |
AssertionError | Assertion statement failure |
AttributeError | Object does not have this property |
BaseException | The base class for all exceptions |
DeprecationWarning | Warnings about deprecated features |
EnvironmentError | Base class for operating system errors |
EOFError | No built-in input, EOF tag is reached |
Exception | Base class for general errors |
FloatingPointError | Floating point error |
FutureWarning | A warning about future semantic changes to the construct |
GeneratorExit | An exception occurred on the generator to indicate exit |
ImportError | Failed to import the module or object. Procedure |
IndentationError | The indentation error |
IndexError | There is no such index in the sequence. |
IOError | The input/output operation failed |
KeyboardInterrupt | The user interrupts execution (usually by typing ^C) |
KeyboardInterrupt | The user interrupts execution (usually by typing ^C) |
KeyError | That’s not in the map |
LookupError | The base class for invalid data queries |
MemoryError | Out-of-memory errors (not fatal to the Python interpreter) |
NameError | Undeclared/initialized objects (no attributes) |
NotImplementedError | Methods that have not yet been implemented |
OSError | Operating system error |
OverflowError | Numerical calculation exceeds the maximum limit |
OverflowWarning | Old warning about automatic promotion to long |
PendingDeprecationWarning | A warning that the feature will be deprecated |
ReferenceError | Weak references attempt to access objects that have already been garbage collected |
RuntimeError | General runtime errors |
RuntimeWarning | Warning about suspicious runtime behavior |
StandardError | Base class for all built-in standard exceptions |
StopIteration | Iterators have no more values |
SyntaxError | Python syntax error |
SyntaxWarning | Warnings about questionable syntax |
SystemError | General interpreter system error |
SystemExit | The interpreter requests to exit |
SystemExit | The Python interpreter requests to exit |
TabError | Use Tab and space together |
TypeError | An invalid operation for a type |
UnboundLocalError | Access an uninitialized local variable |
UnicodeDecodeError | Error in Unicode decoding |
UnicodeEncodeError | Unicode encoding error |
UnicodeError | Unicode-related error |
UnicodeTranslateError | Unicode conversion error |
UserWarning | Warnings generated by user code |
ValueError | Invalid argument passed |
Warning | The base class for warnings |
WindowsError | System call failure |
ZeroDivisionError | Divide (or mod) by zero (all data types) |