Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'causing': 0.04; 'syntax': 0.04; 'debugging': 0.07; 'bug.': 0.09; 'exception,': 0.09; 'indicates': 0.09; 'mask': 0.09; 'part,': 0.09; 'pep': 0.09; 'spelled': 0.09; 'used.': 0.09; 'bug': 0.12; 'mostly': 0.14; '(arnold': 0.16; ':-(': 0.16; '>on': 0.16; '>the': 0.16; '>try:': 0.16; '_macbeth_': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'hell.': 0.16; 'imo,': 0.16; 'indexerror:': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'simpson': 0.16; 'slater': 0.16; 'spotted': 0.16; 'typo': 0.16; 'url:peps': 0.16; 'exception': 0.16; 'do,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'examples': 0.20; 'seems': 0.21; 'command': 0.22; 'header:User-Agent:1': 0.23; 'url:dev': 0.24; 'cheers,': 0.24; "haven't": 0.24; 'looks': 0.24; '(or': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; "doesn't": 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'usually': 0.31; 'bad.': 0.31; 'catching': 0.31; "d'aprano": 0.31; 'exceptions': 0.31; 'steven': 0.31; 'this.': 0.32; 'url:python': 0.33; 'bugs': 0.33; 'plain': 0.33; 'actual': 0.34; 'subject:with': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'i.e.': 0.36; 'skip:> 10': 0.36; 'skip:j 20': 0.36; 'charset :us-ascii': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'thank': 0.38; 'ahead': 0.38; 'skip:m 40': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'little': 0.38; 'channel': 0.39; 'to:addr:python.org': 0.39; 'catch': 0.60; 'eye': 0.61; 'john': 0.61; 'times': 0.62; 'content-disposition:inline': 0.62; 'such': 0.63; 'more': 0.64; 'finish': 0.65; 'relatively': 0.65; 'between': 0.67; 'real-world': 0.68; 'wish': 0.70; 'special': 0.74; 'attractive': 0.81; '2015': 0.84; 'bare': 0.84; 'interrupt': 0.84; 'newbie,': 0.84; 'pain': 0.84; 'received:192.168.15': 0.84; 'subject:..': 0.84; 'taken.': 0.84; 'lazy': 0.91; 'silent': 0.95 Date: Sun, 12 Apr 2015 16:47:05 +1000 From: Cameron Simpson To: python-list@python.org Subject: Re: try..except with empty exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <5529f229$0$13010$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.23 (2014-03-12) References: <5529f229$0$13010$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428821229 news.xs4all.nl 2936 [2001:888:2000:d::a6]:44128 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:88860 On 12Apr2015 14:18, Steven D'Aprano wrote: >On Sun, 12 Apr 2015 09:08 am, Cameron Simpson wrote: >> Also, IMO, a bare "except:" syntax is far more pleasing to the eye than >> "except magic_exception_name_that+gets_everything:". > >And that is exactly what makes bare excepts an attractive nuisance! > >I'm going to channel a newbie, cowboy or just plain lazy coder: >"I have a block of code with a bug. Bugs are bad. I know, I'll wrap it in > >try: > block >except: > pass > >and the bug will go away!" And haven't we all seen that _many_ times on this list:-) >It looks nice and requires relatively little typing. It seems like something >you should do, but it doesn't fix the problem, chances are it just makes it >worse. And do not get me started on Makefiles which finish every compile command in "2>/dev/null" :-( >I've spotted many real-world examples where bare excepts mask the >presence of actual bugs, where the intention is clearly to catch a single >exception: [...] >Forcing people to type an exception will discourage such cowboy coding. If >the choice is between > > except BaseException: > >instead of the lazy "except:" version, and > > except IndexError: > >which do you think people will write? I fear that the just-beyond-beginner might write "Exception", but your point is well taken. >> Also, I wish >> "BaseException" were just spelled "Exception", if it has to be used. > >Most of the time, "catch everything" should mean catching Exception, not >BaseException. Broadly speaking, built-in exceptions which are >considered "errors" inherit from Exception, and the small number that don't >are used for some variation of control-flow: > >StopIteration >GeneratorExit >KeyboardInterrupt >SysExit > >Most of the time when people say "catch everything" they mean catch all >errors, i.e. Exception, rather than "don't let the user interrupt the code >using KeyboardInterrupt". Yes, I see that this is true. And I have special pain in some of my code which then causes me to specificly catch NameError ahead of my bare (or mostly bare) excepts because that usually indicates some stupid typo on my part, causing silent breakage and debugging hell. Ghastly. >See also PEP 352: >http://www.python.org/dev/peps/pep-0352/ Thank you for this. Cheers, Cameron Simpson To be or not to be? Not to be. - John Slater (Arnold Schwartzenegger), _Macbeth_ (_The_Last_Action_Hero_)