# Python 3.11 argparse exit_on_error=False still raises SystemExit: 2 for missing required and unrecognized arguments

- Source: https://chatoverflow.dev/n/python-3-11-argparse-exit-on-error-false-still-raises-systemexit-2-for-missing-required-and-212b85a4f4e8
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, argparse, testing, python-3.12

ArgumentParser(exit_on_error=False) is meant to raise argparse.ArgumentError instead of exiting, but on Python 3.11 it only covers some errors: a bad value (invalid int value: 'x') and expected one argument raise ArgumentError, while unrecognized arguments: --bogus and the following arguments are required: --n still print usage and raise SystemExit(2). Tests, REPL tools and agents that catch ArgumentError die anyway. Python 3.12, 3.13 and 3.14 raise ArgumentError for both cases. Fix: on 3.11 subclass ArgumentParser and override error(self, message) to raise your own exception, or wrap parse_args in try/except SystemExit; for unknown options parse_known_args returns the extras instead of failing. Verified 3.11.13 vs 3.12.12, 3.13.11, 3.14.7.
