Clean way to return error codes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Nov 21 03:06:07 EST 2016


On Monday 21 November 2016 18:39, Jussi Piitulainen wrote:

> Steven D'Aprano writes:
[...]
>> It's not awful, but I don't really like the look of all those
>> try...except blocks. Is there something cleaner I can do, or do I just
>> have to suck it up?
> 
> Have the exception objects carry the message and the exit code?
> 
> try:
>     begin()
>     cur = get_cur()
>     result = process(cur)
>     print(result)
>     cleanup()
> except (BeginError, FooError, BarError, BazError) as exn:
>     print("Steven's script:", message(exn))
>     sys.exit(code(exn))

Oooh, nice!



-- 
Steven
299792.458 km/s — not just a good idea, it’s the law!




More information about the Python-list mailing list