- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 will learn about what an exception is before learning how to print python exceptions.
An exception occurs when a program fails to execute in the direction it was intended to be. Python throws exceptions when unexpected errors or events occur.
It is common for exceptions to be both valid and invalid. Errors and exceptional conditions can be managed in a program through exceptions in a variety of ways. The exception handling technique can be used when you suspect your code may create an error. This can prevent the software from crashing.
Common Exceptions
IOError (Input Output Error) − When the file cannot be opened
ImportError − When Python cannot find the module
ValueError − Occurs when the user hits the interrupt key (normally ctrl+c or delete)
EOFError (End of File Error) − Caused when the input() or raw_input() encounters an end-of-file condition (EOF) without reading any data.
Python Exception/Error Hierarchy
Python Exception hierarchy consists of various built-in exceptions. This hierarchy is used to handle various types of exceptions, as the concept of inheritance also comes into picture.
In Python, all the built-in exceptions must be the instances of a class derived from BaseException.
This Exception or Error hierarchy can be printed by importing the Python's inspect module. Using the inspect module, one can perform type checking, retrieve the source code of a method, inspects classes and functions, and examine interpreter stack.
Here in this case, the getclasstree() function from the inspect module can be used to build a tree hierarchy.
Syntax
inspect.getclasstree(classes, unique = False)
Example
A hierarchy of nested lists of classes can be arranged using inspect.getclasstree(). Where the nested list appears, the classes that derive from the class immediately following will appear as well.
# Inbuilt exceptions: # Import the inspect module import inspect as ipt def tree_class(cls, ind = 0): print ('-' * ind, cls.__name__) for K in cls.__subclasses__(): tree_class(K, ind + 3) print ("Inbuilt exceptions is: ") # THE inspect.getmro() will return the tuple. # of class which is cls's base classes. #The next step is to create a tree hierarchy. ipt.getclasstree(ipt.getmro(BaseException)) # function call tree_class(BaseException)
Output
In this example, we only printed the hierarchy of BaseException; to print the hierarchy of other Exceptions, pass “Exception” as an argument to the function.
Inbuilt exceptions is: BaseException --- Exception ------ TypeError ------ StopAsyncIteration ------ StopIteration ------ ImportError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ------ TokenError ------ StopTokenizing ------ ClassFoundException ------ EndOfBlock --- GeneratorExit --- SystemExit --- KeyboardInterrupt
Example
Another example used to display the Python Exception/Error hierarchy is as follows. Here, we are trying to display the hierarchy of other exceptions using “Exception” −
import inspect print("The class hierarchy for built-in exceptions is:") inspect.getclasstree(inspect.getmro(Exception)) def classtree(cls, indent=0): print('.' * indent, cls.__name__) for subcls in cls.__subclasses__(): classtree(subcls, indent + 3) classtree(Exception))
Output
The output is printed as follows −
The class hierarchy for built-in exceptions is: Exception ... TypeError ... StopAsyncIteration ... StopIteration ... ImportError ...... ModuleNotFoundError ...... ZipImportError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... Verbose ... TokenError ... StopTokenizing ... EndOfBlock
Conclusion
As a result of this article, we learned how to print the exception error in the hierarchy using Python's inspect module.