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


Groups > comp.lang.python > #88772 > unrolled thread

try..except with empty exceptions

Started byPavel S <pavel@schon.cz>
First post2015-04-10 01:48 -0700
Last post2015-04-10 18:56 -0700
Articles 4 on this page of 24 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  try..except with empty exceptions Pavel S <pavel@schon.cz> - 2015-04-10 01:48 -0700
    Re: try..except with empty exceptions Chris Angelico <rosuav@gmail.com> - 2015-04-10 18:58 +1000
    Re: try..except with empty exceptions sohcahtoa82@gmail.com - 2015-04-10 12:31 -0700
      Re: try..except with empty exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-11 11:42 +1000
        Re: try..except with empty exceptions Dave Angel <davea@davea.name> - 2015-04-10 22:23 -0400
          Re: try..except with empty exceptions Rustom Mody <rustompmody@gmail.com> - 2015-04-10 19:38 -0700
            Re: try..except with empty exceptions Dave Angel <davea@davea.name> - 2015-04-10 23:46 -0400
              Re: try..except with empty exceptions Rustom Mody <rustompmody@gmail.com> - 2015-04-10 21:17 -0700
                Re: try..except with empty exceptions Rustom Mody <rustompmody@gmail.com> - 2015-04-10 21:39 -0700
            Re: try..except with empty exceptions Cameron Simpson <cs@zip.com.au> - 2015-04-11 19:27 +1000
              Re: try..except with empty exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-11 21:00 +1000
                Re: try..except with empty exceptions Chris Angelico <rosuav@gmail.com> - 2015-04-11 21:21 +1000
                Re: try..except with empty exceptions Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-11 12:49 -0600
                Re: try..except with empty exceptions Chris Angelico <rosuav@gmail.com> - 2015-04-12 06:04 +1000
                Re: try..except with empty exceptions Chris Angelico <rosuav@gmail.com> - 2015-04-12 06:05 +1000
          Re: try..except with empty exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-11 17:11 +1000
            Re: try..except with empty exceptions Serhiy Storchaka <storchaka@gmail.com> - 2015-04-11 11:22 +0300
              Re: try..except with empty exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-11 20:47 +1000
            Re: try..except with empty exceptions Dave Angel <davea@davea.name> - 2015-04-11 06:14 -0400
            Re: try..except with empty exceptions Dave Angel <davea@davea.name> - 2015-04-11 06:24 -0400
            Re: try..except with empty exceptions Ian Foote <ian@feete.org> - 2015-04-11 15:20 +0100
        Re: try..except with empty exceptions Terry Reedy <tjreedy@udel.edu> - 2015-04-11 04:58 -0400
    Re: try..except with empty exceptions Dave Angel <davea@davea.name> - 2015-04-10 16:27 -0400
    Re: try..except with empty exceptions Rustom Mody <rustompmody@gmail.com> - 2015-04-10 18:56 -0700

Page 2 of 2 — ← Prev page 1 [2]


#88820

FromIan Foote <ian@feete.org>
Date2015-04-11 15:20 +0100
Message-ID<mailman.221.1428762375.12925.python-list@python.org>
In reply to#88806
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11/04/15 08:11, Steven D'Aprano wrote:
> But with try...except, an empty exception list means to catch
> *everything*, not nothing:
> 
> try: ... except a,b,c: # catches a, b, c
> 
> try: ... except a,b: # catches a, b

This example is incorrect. In python3 it is a SyntaxError:

    Python 3.4.0 (default, Apr 11 2014, 13:05:11)
    [GCC 4.8.2] on linux
    Type "help", "copyright", "credits" or "license" for more
    information.
    >>> try:
    ...  1/0
    ... except ValueError, ZeroDivisionError:
      File "<stdin>", line 3
        except ValueError, ZeroDivisionError:
                         ^
    SyntaxError: invalid syntax

In python2 it aliases ValueError as ZeroDivisionError:

    Python 2.7.6 (default, Mar 22 2014, 22:59:56)
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more
    information.
    >>> try:
    ...  1/0
    ... except ValueError, ZeroDivisionError:
    ...  pass
    ...
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    ZeroDivisionError: integer division or modulo by zero

To get the behaviour you expect, you must use parentheses:

    >>> try:
    ...  1/0
    ... except (ValueError, ZeroDivisionError):
    ...  pass
    ...

Regards,
Ian F
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJVKS2dAAoJEODsV4MF7PWznkAH/jidWhoJ//gsvBr0ByOOOEgc
A+k8HqrkALfzrh3aEjJB3sq19oLfLcepQeFVUh77mJKOMCQdEeyJtqIz6tLc4RUa
L/nXytHygXVTb5HIARGVkPD26gqAleSb9eZUfPeSEvRHy9UbFS7SMmOdkApheDX3
Vq8TOa8EchaYd+S89y9eepZAhGC7n2TNwrNgp36sbHoz/hYUxFNnugP0ow9FM0Wk
MGKGh04c3Lao+6w7a0scz4YKKb8wTdYkyYwlJhEdg3q74+PwYJpkjcGucna745AZ
XlAKlDCJ9LhPgMufuGdRNskJa4TF709ec5hG9itHu1lFKrjH1iJCEU9ntX6hInU=
=zi4K
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#88811

FromTerry Reedy <tjreedy@udel.edu>
Date2015-04-11 04:58 -0400
Message-ID<mailman.216.1428742735.12925.python-list@python.org>
In reply to#88792
On 4/10/2015 9:42 PM, Steven D'Aprano wrote:

> try:
>      spam()
> except:
>      # Implicitly an empty tuple.
>      pass

No, specified as equivalent to 'except BaseException:' (or 'except 
(BaseException,):', either of which are different from 'except ():'.
"An expression-less except clause, if present, must be last; it matches 
any exception."

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#88786

FromDave Angel <davea@davea.name>
Date2015-04-10 16:27 -0400
Message-ID<mailman.208.1428697681.12925.python-list@python.org>
In reply to#88772
On 04/10/2015 04:48 AM, Pavel S wrote:
> Hi,
>
> I noticed interesting behaviour. Since I don't have python3 installation here, I tested that on Python 2.7.
>
> Well known feature is that try..except block can catch multiple exceptions listed in a tuple:
>
>
> exceptions = ( TypeError, ValueError )
>
> try:
>      a, b = None
> except exceptions, e:
>      print 'Catched error:', e
>
>
> However when exceptions=(), then try..except block behaves as no try..except block.
>
>
> exceptions = ()
>
> try:
>      a, b = None   # <--- the error will not be catched
> except exceptions, e:
>      print 'Catched error:', e
>
>
> I found use case for it, e.g. when I want to have a method with 'exceptions' argument:
>
>
> def catch_exceptions(exceptions=()):
>    try:
>       do_something()
>    except exceptions:
>       do_something_else()
>
>
> catch_exceptions()               # catches nothing
> catch_exceptions((TypeError,))   # catches TypeError
>
>
> I believe that behaviour is not documented. What you think?
>

It's no more surprising than a for loop over an empty tuple or empty 
list.  There's nothing to do, so you do nothing.



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#88794

FromRustom Mody <rustompmody@gmail.com>
Date2015-04-10 18:56 -0700
Message-ID<fd7df6dc-e18e-488f-8258-caa38ee6cd31@googlegroups.com>
In reply to#88772
On Friday, April 10, 2015 at 2:18:22 PM UTC+5:30, Pavel S wrote:
> Hi,
> 
> I noticed interesting behaviour. Since I don't have python3 installation here, I tested that on Python 2.7.
> 
> Well known feature is that try..except block can catch multiple exceptions listed in a tuple:
> 
> 
> exceptions = ( TypeError, ValueError )
> 
> try:
>     a, b = None
> except exceptions, e:
>     print 'Catched error:', e
> 
> 
> However when exceptions=(), then try..except block behaves as no try..except block.
> 
> 
> exceptions = ()
> 
> try:
>     a, b = None   # <--- the error will not be catched
> except exceptions, e:
>     print 'Catched error:', e
> 
> 
> I found use case for it, e.g. when I want to have a method with 'exceptions' argument:
> 
> 
> def catch_exceptions(exceptions=()):
>   try:
>      do_something()
>   except exceptions:
>      do_something_else()
> 
> 
> catch_exceptions()               # catches nothing
> catch_exceptions((TypeError,))   # catches TypeError
> 
> 
> I believe that behaviour is not documented. What you think?

As others have pointed out: "You asked for it; you got it; what's the issue?"

Nevertheless a tentative +1 from me on the suggestions ∵

except :

catches everything 

except ():

catches nothing --- which is brittle to say the least.

Also given this sort of lines in the docs (2.7 tutorial):

-----------------------------
... except (RuntimeError, TypeError, NameError):
...     pass

Note that the parentheses around this tuple are required, because except
ValueError, e: was the syntax used for what is normally written as except
ValueError as e: in modern Python (described below). The old syntax is still 
supported for backwards compatibility. This means except RuntimeError, 
TypeError is not equivalent to except (RuntimeError, TypeError): but to except 
RuntimeError as TypeError: which is not what you want.
------------------------------------

there's already some versioning related brittleness around except.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web