There might arise a situation where there is a need for additional information from an exception raised by Python.
Python has two types of exceptions namely, Built-In Exceptions and User-Defined Exceptions.
Why use Argument in Exceptions?
Using arguments for Exceptions in Python is useful for the following reasons:
- It can be used to gain additional information about the error encountered.
- As contents of an Argument can vary depending upon different types of Exceptions in Python, Variables can be supplied to the Exceptions to capture the essence of the encountered errors. Same error can occur of different causes, Arguments helps us identify the specific cause for an error using the except clause.
- It can also be used to trap multiple exceptions, by using a variable to follow the tuple of Exceptions.
Arguments in Buil-in Exceptions:
The below codes demonstrates use of Argument with Built-in Exceptions:
This is the Argument division by zero
This is the Argument unsupported operand type(s) for /: 'str' and 'int'
Arguments in User-defined Exceptions:
The below codes demonstrates use of Argument with User-defined Exceptions:
This is the Argument 'Some Error Data'
Exception occurred: (2, 6, 'Not Allowed')
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.