2012-03-31 1 views
4

は、あなたは、例外のリストを表示するには、この操作を行うことができます。Python 3.xで考えられる例外のリストを取得する方法はありますか? Pythonの2.7で

import exceptions 

    for i in dir(exceptions): 
    print (i) 

は、Python 3.2のための同様のものがありますか?モジュールには例外はありません。私はあなたが例外について反復することができることは承知していますが、それはあなたに可能なすべての例外のリストを与えるものではありません。あなたは組み込み例外のリストを取得するには(かなりが、作品ではない)、このような何かを行うことができ

+0

BeautifulSoup 4が必要です)(例外のない固定セットがありません;。新しいものを定義することができます)これらの –

答えて

3

>>> exes = [ex for ex in vars(__builtins__).values() 
...   if hasattr(ex, '__mro__') and issubclass(ex, BaseException)] 
>>> exes 
[<class 'IndexError'>, <class 'SyntaxError'>, <class 'UnicodeDecodeError'>, 
<class 'NameError'>, <class 'BytesWarning'>, <class 'IOError'>, <class 'SystemExit'>, 
<class 'RuntimeWarning'>, <class 'Warning'>, <class 'UnicodeTranslateError'>, 
<class 'EOFError'>, <class 'BufferError'>, <class 'FloatingPointError'>, 
<class 'FutureWarning'>, <class 'ImportWarning'>, <class 'ReferenceError'>, 
<class 'TypeError'>, <class 'KeyboardInterrupt'>, <class 'UserWarning'>, 
<class 'ResourceWarning'>, <class 'SystemError'>, <class 'BaseException'>, 
<class 'RuntimeError'>, <class 'MemoryError'>, <class 'StopIteration'>, 
<class 'LookupError'>, <class 'UnicodeError'>, <class 'ImportError'>, 
<class 'Exception'>, <class 'UnicodeEncodeError'>, <class 'SyntaxWarning'>, 
<class 'ArithmeticError'>, <class 'GeneratorExit'>, <class 'KeyError'>, 
<class 'PendingDeprecationWarning'>, <class 'EnvironmentError'>, <class 'OSError'>, 
<class 'DeprecationWarning'>, <class 'UnicodeWarning'>, <class 'ValueError'>, 
<class 'TabError'>, <class 'ZeroDivisionError'>, <class 'IndentationError'>, 
<class 'AssertionError'>, <class 'UnboundLocalError'>, <class 'NotImplementedError'>, 
<class 'AttributeError'>, <class 'OverflowError'>] 
2

これはどちらか間違いなく良い解決策はありませんが、それは一種のクールです。

>>> from urllib.request import urlopen 
>>> from bs4 import BeautifulSoup 
>>> 
>>> data = urlopen('http://docs.python.org/py3k/library/exceptions.html').read() 
>>> parsed = BeautifulSoup(data) 
>>> exceptions = [x.text for x in parsed.select('dl.exception > dt tt.descname')] 
>>> exceptions 
['BaseException', 'Exception', 'ArithmeticError', 'BufferError', 'LookupError', 
'EnvironmentError', 'AssertionError', 'AttributeError', 'EOFError', 
'FloatingPointError', 'GeneratorExit', 'IOError', 'ImportError', 'IndexError', 
'KeyError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'NotImplementedError', 
'OSError', 'OverflowError', 'ReferenceError', 'RuntimeError', 'StopIteration', 
'SyntaxError', 'IndentationError', 'TabError', 'SystemError', 'SystemExit', 
'TypeError', 'UnboundLocalError', 'UnicodeError', 'UnicodeEncodeError', 
'UnicodeDecodeError', 'UnicodeTranslateError', 'ValueError', 'VMSError', 
'WindowsError', 'ZeroDivisionError', 'Warning', 'UserWarning', 'DeprecationWarning', 
'PendingDeprecationWarning', 'SyntaxWarning', 'RuntimeWarning', 'FutureWarning', 
'ImportWarning', 'UnicodeWarning', 'BytesWarning', 'ResourceWarning'] 

+0

両方が私が住むことができる少し余分なジャンクで、探していた何をすべきかと。ありがとう! – cyd

+0

@cyd、他の答えは良いです:それはhtmlを掻き集めることを伴わない。 – huon

関連する問題