Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'clause': 0.09; 'derived': 0.09; 'exit': 0.09; 'iterate': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'unhandled': 0.09; 'subject:How': 0.10; 'jan': 0.12; '"user': 0.16; 'clause,': 0.16; 'exit.': 0.16; 'marco': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'systemexit': 0.16; 'unexpected': 0.16; 'user-defined': 0.16; 'exception': 0.16; 'wrote:': 0.18; '(in': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'switched': 0.24; 'why.': 0.24; 'class.': 0.26; 'nearly': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'included': 0.31; "d'aprano": 0.31; 'exceptions': 0.31; 'steven': 0.31; 'class': 0.32; 'except': 0.35; 'but': 0.35; 'thanks': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'clear': 0.37; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'catch': 0.60; 'is.': 0.60; 'manually': 0.60; 'received:173': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'relatively': 0.65; 'details': 0.65; 'answer:': 0.84; 'does?': 0.84; 'everything,': 0.84; 'received:fios.verizon.net': 0.84; 'subject:skip:S 20': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How come StopIteration.__base__ is not BaseException? Date: Tue, 27 Aug 2013 14:51:59 -0400 References: <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> <521C7935.8080205@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: <521C7935.8080205@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377629540 news.xs4all.nl 15942 [2001:888:2000:d::a6]:50412 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53084 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