args (exception)
The tuple of arguments an exception object was created with.
In more detail
Every exception stores the arguments passed to it in a tuple called args. Printing the exception shows str(e), which is empty for no arguments, the argument itself for one, and the whole tuple for two or more. A custom exception keeps args even when its own __init__ never calls super().
Example
try:
raise ValueError('bad', 42)
except ValueError as e:
print(e.args) # ('bad', 42)Related terms
- exception — An error object a program raises to stop normal flow and report that something went wrong.
- raise statement — The statement that signals an error by creating and throwing an exception, as in raise ValueError("width must be positive").
- custom exception — An exception class you define yourself, usually in two lines by subclassing Exception, to name an error specific to your program.
Taught in
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Handlers, args, raise And assert — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep