Reputation: 41
In Python 2.7, you can do this to see a list of exceptions:
import exceptions
for i in dir(exceptions):
print (i)
Is there something similar for Python 3.2? There's no module "exceptions" for one thing. I know you can iterate on Exception, but that doesn't give you a list of all the possible exceptions, which is what I'm looking for.
Upvotes: 4
Views: 183
Reputation: 352979
You could do something like this (not pretty, but works) to get a list of the builtin exceptions:
>>> 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'>]
Upvotes: 3
Reputation: 102016
This is definitely not a good solution either, but it's sort of cool.
>>> 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']
(Requires BeautifulSoup 4)
Upvotes: 2