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


Groups > comp.lang.python > #88773

Re: try..except with empty exceptions

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'python3': 0.07; 'clause': 0.09; 'definition,': 0.09; 'pavel': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; 'behaviour.': 0.16; 'block.': 0.16; 'easier.': 0.16; 'exceptions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'skipping': 0.16; 'think?': 0.16; 'tuple': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; '(where': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'installation': 0.23; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'feature': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '3.x': 0.31; 'consequence': 0.31; 'exceptions': 0.31; 'there.': 0.32; 'compatible': 0.32; 'fri,': 0.33; 'actual': 0.34; 'noticed': 0.34; 'subject:with': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'hi,': 0.36; 'pm,': 0.38; 'sure': 0.39; 'even': 0.60; 'catch': 0.60; "you're": 0.61; 'different': 0.65; 'side': 0.67; 'believe': 0.68; 'natural': 0.68; 'production': 0.68; 'hesitate': 0.70; '2.7.': 0.84; '2015': 0.84; 'subject:..': 0.84; 'thrown,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=mECd7qVb899ghPJ6yoHxC8qBsmAN5xXur3jt89YeGXM=; b=G+9zPxNlmDizDTdmW1Y0WB/QUONJsHA0wvGLh/BDMLpWexQQ6Tnmk6G29W5GMc2QDD 8lN17Q57F++Yr7KHKE85KfQwuX8OBjvqOp1+GSJ1lMNLanxmG5NSt/yyAC/6qK6KWikT tUL3kcvW544yZo8Vlz4oyGlPTOxYXnxXvRp3elpPXExHCUGtZXrf4nb55j6PD8O0UNpu 50RVBYUHDd6dYjNSxjwMn0KM6SHIi6IzyijHP8TzX99AfpaBd9lgw5aYxo1YLsctQYn5 xpjXRjw8zP4ymHwol+8EGHlnSQzCQ74JLbE4McsaMz8BXh7ATmOsB86TerBIVQnKcaPJ 1V/g==
MIME-Version 1.0
X-Received by 10.107.16.32 with SMTP id y32mr799483ioi.53.1428656303240; Fri, 10 Apr 2015 01:58:23 -0700 (PDT)
In-Reply-To <64a75c32-e0ab-4ce0-9373-358c2669fe6e@googlegroups.com>
References <64a75c32-e0ab-4ce0-9373-358c2669fe6e@googlegroups.com>
Date Fri, 10 Apr 2015 18:58:23 +1000
Subject Re: try..except with empty exceptions
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.198.1428656305.12925.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1428656305 news.xs4all.nl 2850 [2001:888:2000:d::a6]:43840
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88773

Show key headers only | View raw


On Fri, Apr 10, 2015 at 6:48 PM, Pavel S <pavel@schon.cz> 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:
>
> 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 believe that behaviour is not documented. What you think?

It's no different from any other except clause that doesn't match. An
empty tuple of exception types can never match any actual exception
thrown, so it'll be like skipping that except block. It doesn't need
to be specifically documented, as it's a natural consequence of the
use of the empty tuple there.

However, even though it's perfectly plausible as regards language
definition, I would hesitate to use it in production code. I'm not
sure this makes your code any better.

Side point: Even though you're not using Python 3 (where it's
mandatory), I would recommend using the "as" syntax with except
clauses:

exceptions = ()
try:
    a, b = None
except exceptions as e:
    print("Can't happen")

Writing your code to be compatible with both 2.7 and 3.x will make the
eventual port easier.

ChrisA

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


Thread

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

csiph-web