How to exit program with custom code and custom message?

Chris Angelico rosuav at gmail.com
Tue Mar 14 00:58:39 EDT 2023


On Tue, 14 Mar 2023 at 15:28, Thomas Passin <list1 at tompassin.net> wrote:
>
> On 3/13/2023 10:34 PM, scruel tao wrote:
> > Interesting, `raise SystemExit` seems to have the same behavior as `sys.exit`:
> >
> > ```shell
> > python -c "raise SystemExit(100)"
> > echo $?
> > <<< 100
> >
> > python -c " import sys; sys.exit(100)"
> > echo $?
> > <<< 100
>
> OTOH, you don't want to get too tricky:
>
> (on Windows, obviously)
> C:\Users\tom>py -c "import sys; sys.exit(type(100) == type('a'))"
>
> C:\Users\tom>echo %ERRORLEVEL%
> 0
>
> Presumably we wouldn't want to get an exit value of 0 for this case!
>

Why? You passed the value False, which is 0. So it should behave as
documented, and exit 0.

ChrisA


More information about the Python-list mailing list