Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53084

Re: How come StopIteration.__base__ is not BaseException?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: How come StopIteration.__base__ is not BaseException?
Date 2013-08-27 14:51 -0400
References <kvgapp$lh3$1@speranza.aioe.org> <mailman.246.1377547832.19984.python-list@python.org> <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> <521C7935.8080205@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.278.1377629540.19984.python-list@python.org> (permalink)

Show all headers | View raw


On 8/27/2013 6:02 AM, Marco Buttu wrote:
> 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.

Until relatively recently, in 2.5, Exception *was* the base exception 
class and for nearly everything, it still is. "All built-in, 
non-system-exiting exceptions are derived from this class. All 
user-defined exceptions should also be derived from this class." 
BaseException was added just so it would be possible to catch nearly 
everything but a few exceptions. The first two were KeyboardInterrupt 
and SystemExit (in 2.5). GeneratorExit was switched in 2.6, but I forget 
the details of why.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How come StopIteration.__base__ is not BaseException? Marco Buttu <marco.buttu@gmail.com> - 2013-08-26 21:37 +0200
  Re: How come StopIteration.__base__ is not BaseException? random832@fastmail.us - 2013-08-26 16:10 -0400
    Re: How come StopIteration.__base__ is not BaseException? Marco Buttu <marco.buttu@gmail.com> - 2013-08-27 08:17 +0200
      Re: How come StopIteration.__base__ is not BaseException? Marco Buttu <marco.buttu@gmail.com> - 2013-08-27 08:18 +0200
      Re: How come StopIteration.__base__ is not BaseException? Steven D'Aprano <steve@pearwood.info> - 2013-08-27 09:22 +0000
        Re: How come StopIteration.__base__ is not BaseException? Marco Buttu <marco.buttu@gmail.com> - 2013-08-27 12:02 +0200
          Re: How come StopIteration.__base__ is not BaseException? Terry Reedy <tjreedy@udel.edu> - 2013-08-27 14:51 -0400
            Re: How come StopIteration.__base__ is not BaseException? Marco Buttu <marco.buttu@gmail.com> - 2013-08-27 21:52 +0200
              Re: How come StopIteration.__base__ is not BaseException? Terry Reedy <tjreedy@udel.edu> - 2013-08-27 16:37 -0400
  Re: How come StopIteration.__base__ is not BaseException? Ethan Furman <ethan@stoneleaf.us> - 2013-08-27 13:00 -0700

csiph-web