程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python custom exception and common exception types

編輯:Python

about try…except…else…finally

def self_error(type_list):
""" Exception handling functions Pass in an iteratable object , Handle parameter incoming error conditions :paramtype_list: An iteratable object """
try:
for i in type_list:
print(i)
except:
print(" It's not an iterative object ")
else:
print(" There was no mistake ")
finally:
print(" End of program running ")
self_error("abc")
""" a b c There was no mistake End of program running """
self_error(2)
""" It's not an iterative object End of program running """

It can be seen from the program that if try Partial smooth implementation , Will perform else part , If try If there are some errors, it will execute except part , But both cases are finally output finally part . Of course ,else and finally Part can be omitted . We can customize the output error type , Need to use raise function .

def self_error(type_num):
""" Exception handling functions Custom exception function , Used to throw an exception :paramtype_num: The value used to determine whether an exception is thrown :return Abnormal information """
if type_num<10:
raise ValueError(" Value is less than 10")
else:
return 200
if __name__=="__main__":
self_error(11) #200
self_error(9) #ValueError: Value is less than 10

raise Throwing an exception only applies to python Standard exception class of
Exception name describe
ArithmeticError Base class for all numerical errors
AssertionError Assertion statement failed
AttributeError Object does not have this property
BaseException The base class for all exceptions
DeprecationWarning A warning about abandoned features
EnvironmentError Base class for operating system errors
EOFError No built-in input , arrive EOF Mark
Exception Regular error base class
FloatingPointError Floating point error
FutureWarning A warning about future semantic changes in construction
GeneratorExit generator (generator) An exception occurs to notify the exit
ImportError The import module / Object failed
IndentationError The indentation error
IndexError There is no such index in the sequence (index)
IOError Input / Output operation failed
KeyboardInterrupt User interrupt execution ( Usually the input ^C)
KeyboardInterrupt User interrupt execution ( Usually the input ^C)
KeyError There is no key in the map
LookupError Base class for invalid data query
MemoryError Memory overflow error ( about Python The interpreter is not fatal )
NameError Not a statement / Initialize object ( There is no attribute )
NotImplementedError A method that has not yet been implemented
OSError Operating system error
OverflowError The numerical operation exceeds the maximum limit
OverflowWarning Old about auto promotion to long form (long) Warning of
PendingDeprecationWarning A warning that features will be discarded
ReferenceError Weak reference (Weak reference) Trying to access a garbage collected object
RuntimeError General runtime errors
RuntimeWarning Suspicious runtime behavior (runtime behavior) Warning of
StandardError All built-in standard exception base classes
StopIteration Iterators have no more values
SyntaxError Python Grammar mistakes
SyntaxWarning A dubious grammatical warning
SystemError General interpreter system error
SystemExit The interpreter requests exit
SystemExit Python The interpreter requests exit
TabError Tab Mixed with Spaces
TypeError Invalid operation on type
UnboundLocalError Accessing an uninitialized local variable
UnicodeDecodeError Unicode Error in decoding
UnicodeEncodeError Unicode Error in coding
UnicodeError Unicode Related errors
UnicodeTranslateError Unicode Error in conversion
UserWarning Warnings generated by user code
ValueError Invalid parameter passed in
Warning The base class for warnings
WindowsError System call failed
ZeroDivisionError except ( Or modulus ) zero ( All data types )

Reference link :https://www.jianshu.com/p/e7e123d481f1


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved