Python 3.2+Exceptionsexception
argparse.ArgumentError
module exceptions
An error from creating or using an argument (optional or positional)
Syntax
raise argparse.ArgumentError
You rarely raise it yourself — argparse raises it, for example when two options claim the same flag or add_argument gets contradictory parameters. Inside parse_args it is caught and turned into an error message plus exit code 2, so your try block usually never sees it; to intercept it, subclass ArgumentParser and override error(). The constructor expects two arguments: the offending action (or None) and the message.
Examples
import argparse
try:
raise argparse.ArgumentError(None, 'плохой аргумент')
except argparse.ArgumentError as e:
print(type(e).__name__) # → ArgumentError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy