How come StopIteration.__base__ is not BaseException?

Marco Buttu marco.buttu at gmail.com
Tue Aug 27 06:02:29 EDT 2013


On 08/27/2013 11:22 AM, Steven D'Aprano wrote:
>
> What matters is that when you catch "nearly everything", StopIteration is
> included in the "nearly everything", but SysExit and KeyboardInterrupt
> should not be. Consider:
>
>
> try:
>      main()
> except Exception as e:
>      print('an unexpected error occurred')
>      log_unhandled_exception(e)
>      emergency_shutdown()
>      sys.exit(1)
> except (KeyboardInterrupt, SysExit):
>      # User wants to exit.
>      clean_exit()
>      sys.exit(0)
>
>
>
> Which except clause would you expect an unhandled StopIteration to fall
> under? The unexpected error clause, or the "user wants to exit cleanly"
> clause?

Thanks Steven, that was clear for me. I was thinking about a design 
concept: how come doesn't it inherit directly from BaseException like 
GeneratorExit does? But I think I got the answer: because we can iterate 
manually and so it can propagate, and so we want an except Exception 
clause catches it.

Thanks, Marco

-- 
Marco



More information about the Python-list mailing list