Python実戦のよくある異常とカスタム異常
5276 ワード
下一篇:Python実戦の反射hasattr、getattr、setattr、delattrクリックジャンプカタログ編:python関連カタログ編クリックジャンプ次編:Python実戦のsocket初版Linux粘包問題クリックジャンプ
目次
異常種別常用異常非常用異常異常試験カスタム例外試験異常種別
pythonには異常の種類が非常に多く、各異常は特定の異常を処理するのに特化しています!!!
よくある例外
AttributeError#はfooなどのオブジェクトにない属性にアクセスしようとした.xですがfooには属性xがありません
IOError#入出力異常;基本的にファイルを開けません
ImportError#はモジュールまたはパッケージを導入できません.基本的にはパスの問題または名前のエラーです
IndentationError#構文エラー(のサブクラス);コードが正しく整列していません(インデントの問題)
IndexError#インデックスはシーケンス境界を超えており、例えばxが3つの要素しかないのにxにアクセスしようとしている[5]
KeyError#辞書に存在しないキーにアクセスしようとした
KeyboardInterrupt#Ctrl+Cが押される
NameError#オブジェクトがまだ与えられていない変数を使用
SyntaxError#Pythonコードは不正で、コードはコンパイルできません(個人的には文法エラーと思って書き間違えました)
TypeError#入力オブジェクトタイプと要求の不一致
UnboundLocalError#は、まだ設定されていないローカル変数にアクセスしようとしています.基本的には、同じ名前のグローバル変数があるため、アクセスしていると思っています.
ValueError#は、値のタイプが正しいとしても、呼び出し元が望んでいない値を入力します.
非常用例外
ArithmeticError AssertionError AttributeError BaseException BufferError
BytesWarning DeprecationWarning EnvironmentError EOFError
Exception FloatingPointError FutureWarning GeneratorExit ImportError ImportWarning IndentationError IndexError IOError KeyboardInterrupt
KeyError LookupError MemoryError NameError NotImplementedError
OSError OverflowError PendingDeprecationWarning ReferenceError
RuntimeError RuntimeWarning StandardError StopIteration SyntaxError
SyntaxWarning SystemError SystemExit TabError TypeError
UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError
UnicodeTranslateError UnicodeWarning UserWarning ValueError
Warning ZeroDivisionError
いじょうしけん
目次
異常種別常用異常非常用異常異常試験カスタム例外試験異常種別
pythonには異常の種類が非常に多く、各異常は特定の異常を処理するのに特化しています!!!
よくある例外
AttributeError#はfooなどのオブジェクトにない属性にアクセスしようとした.xですがfooには属性xがありません
IOError#入出力異常;基本的にファイルを開けません
ImportError#はモジュールまたはパッケージを導入できません.基本的にはパスの問題または名前のエラーです
IndentationError#構文エラー(のサブクラス);コードが正しく整列していません(インデントの問題)
IndexError#インデックスはシーケンス境界を超えており、例えばxが3つの要素しかないのにxにアクセスしようとしている[5]
KeyError#辞書に存在しないキーにアクセスしようとした
KeyboardInterrupt#Ctrl+Cが押される
NameError#オブジェクトがまだ与えられていない変数を使用
SyntaxError#Pythonコードは不正で、コードはコンパイルできません(個人的には文法エラーと思って書き間違えました)
TypeError#入力オブジェクトタイプと要求の不一致
UnboundLocalError#は、まだ設定されていないローカル変数にアクセスしようとしています.基本的には、同じ名前のグローバル変数があるため、アクセスしていると思っています.
ValueError#は、値のタイプが正しいとしても、呼び出し元が望んでいない値を入力します.
非常用例外
ArithmeticError AssertionError AttributeError BaseException BufferError
BytesWarning DeprecationWarning EnvironmentError EOFError
Exception FloatingPointError FutureWarning GeneratorExit ImportError ImportWarning IndentationError IndexError IOError KeyboardInterrupt
KeyError LookupError MemoryError NameError NotImplementedError
OSError OverflowError PendingDeprecationWarning ReferenceError
RuntimeError RuntimeWarning StandardError StopIteration SyntaxError
SyntaxWarning SystemError SystemExit TabError TypeError
UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError
UnicodeTranslateError UnicodeWarning UserWarning ValueError
Warning ZeroDivisionError
いじょうしけん
__author__ = "Burgess Zheng"
def bulk(self):
print("%s is yelling...." %self.name)
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print("%s is eating..."%self.name,food)
#d = Dog("NiuHanYang")
#choice = input(">>:").strip()# dd
#getattr(d,choice)#getattr(d,choice) == d.
# d dd d dd
'''
#
names = ['alex','jack']
data = {}
#names[3] # names 3 , names 0 1 ,
#
#data['name'] # data key name data key
# , 。
'''
'''
# KeyError:key IndexError
names = ['alex','jack']
data = {}
try:#
data['name']# data key name, key
names[3]# names 3
except KeyError as e:#KeyError: key
#e:c
print(" key",e)
except IndexError as e:#IndexError:
print(" ",e)
'''
'''
# .
names = ['alex','jack']
data = {}
try:#
names[3]# names 3
data['name']# data key name, key
except (KeyError,IndexError) as e:#e:c
print(" ",e)
# ( names[3] ,
# ,
# , ,
'''
'''
# , Exception :
names = ['alex','jack']
data = {}
try:
names[3] # names 3
data['name'] # data key name, key
except Exception as e:#Exception:
# e:c
print(" ",e)#
# , , ,
'''
# ,
#else: finally:
names = ['alex','jack']
data = {}
try:
#names[3]
#data['name']
#open("tes.txt")
a=1
print(a)
except KeyError as e:#KeyError: key #e:c
print(" key",e)
except IndexError as e: # IndexError: #e:c
print(" ", e)
except Exception as e:#Exception:
print(" ", e)
# , ,
else: #
print(" ")
finally: #
print(" , ")
#
#python3 Python2
#python3:except xxx as e
#python2:except xxx,e
__author__ = "Burgess Zheng"
class BurgessError(Exception):#Exception:
# BurgessError Exception
# BurgessError ,
def __init__(self, msg):
self.message = msg
try:
raise BurgessError(' ')
#(' ') BurgessError msg =
#raise BurgessError
except BurgessError as e:# BurgessError
print(e)#
# , ,
:Python hasattr、getattr、setattr、delattr
:python
:Python socket Linux