- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print the Python Exception/Error Hierarchy?
We import the inspect module and specifically use the getclasstree() function to print the python Exception/Error hierarchy.
This code arranges and prints the given list of exception classes into a hierarchy of nested lists. We recursively go through __subclasses__() down by an inheritance tree as shown in the output.
Example
import inspect print "The class hierarchy for built-in exceptions is:" inspect.getclasstree(inspect.getmro(BaseException)) def classtree(cls, indent=0): print '.' * indent, cls.__name__ for subcls in cls.__subclasses__(): classtree(subcls, indent + 3) classtree(BaseException)
Output
On running the code we get the following output.
The class hierarchy for built-in exceptions is: BaseException ... Exception ...... StandardError ......... TypeError ......... ImportError ............ ZipImportError ......... EnvironmentError ............ IOError ............ OSError ............... WindowsError ......... EOFError ......... RuntimeError ............ NotImplementedError ......... NameError ............ UnboundLocalError ......... AttributeError ......... SyntaxError ............ IndentationError ............... TabError ......... LookupError ............ IndexError ............ KeyError ............ CodecRegistryError ......... ValueError ............ UnicodeError ............... UnicodeEncodeError ............... UnicodeDecodeError ............... UnicodeTranslateError ......... AssertionError ......... ArithmeticError ............ FloatingPointError ............ OverflowError ............ ZeroDivisionError ......... SystemError ............ CodecRegistryError ......... ReferenceError ......... MemoryError ......... BufferError ...... StopIteration ...... Warning ......... UserWarning ......... DeprecationWarning ......... PendingDeprecationWarning ......... SyntaxWarning ......... RuntimeWarning ......... FutureWarning ......... ImportWarning ......... UnicodeWarning ......... BytesWarning ...... _OptionError ...... error ...... Error ...... TokenError ...... StopTokenizing ...... error ...... EndOfBlock ... GeneratorExit ... SystemExit ... KeyboardInterrupt
- Related Articles
- How to capture and print Python exception message?
- Exception Hierarchy in case of multiple catch blocks.
- How to print exception messages in android?
- How do I print a message to the error console using JavaScript?
- Differentiate between exception and error in PHP
- Difference between Exception and Error in Java
- Difference Between Error and Exception in Java
- How to get Python exception text?
- Different ways to print exception messages in Java
- How to handle an exception in Python?
- How to catch KeyError Exception in Python?
- How to raise an exception in Python?
- How to catch IOError Exception in Python?
- How to catch ArithmeticError Exception in Python?
- How to catch OverflowError Exception in Python?

Advertisements