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


Groups > comp.lang.python > #38800

Re: Awsome Python - chained exceptions

Newsgroups comp.lang.python
Date 2013-02-12 18:47 -0800
References <5119de00$0$11096$c3e8da3@news.astraweb.com>
Message-ID <1bc6cf63-9219-4798-ae01-dfe9643d96b4@googlegroups.com> (permalink)
Subject Re: Awsome Python - chained exceptions
From Rick Johnson <rantingrickjohnson@gmail.com>

Show all headers | View raw


On Tuesday, February 12, 2013 12:15:29 AM UTC-6, Steven D'Aprano wrote:
> [snip inflammatory remarks]
> I thought I'd present a few of Python's more
> awesome features, starting with exception contexts.

Well that's great idea, however, in order to find this very "valuable" information the searcher must not only remember an unintuitive search tag, he must also remember /exactly/ how you misspelled the unintuitive search tag! I sure hope you have more of these "Awsome Python"'s to share because i fear this one will surely be forgotten in 2 days. 

> If you've ever written an exception handler, you've probably written a
> *buggy* exception handler:
>
> def getitem(items, index):
>     # One-based indexing.
>     try:
>         return items[index-1]
>     except IndexError:
>         print ("Item at index %d is missing" % index - 1)  # Oops!
>
>
> Unfortunately, when an exception occurs inside an except or finally
> block, the second exception masks the first, and the reason for the
> original exception is lost:
>
> py> getitem(['one', 'two', 'three'], 5)  # Python 2.6
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 6, in getitem
> TypeError: unsupported operand type(s) for -: 'str' and 'int'

But that's exactly what any sane person wants to happen. And i don't know why your error messages are so vague because i got this message (using IDLE):

py> getitem([1,2,3], 5)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    getitem([1,2,3], 5)
  File "<pyshell#1>", line 5, in getitem
    print ("Item at index %d is missing" % index - 1)  # Oops!
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Which (by showing the offensive line) is quite clear to me. I even tried a simplified version on the command line (to make sure IDLE was not injecting anything) and got a better exception message, but sadly without the offending line:

py> try:
...     l[10]
... except IndexError:
...     "{0}".format(blah)
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
NameError: name 'blah' is not defined

But in either case, both forms of the message i got are far more helpful than the exception you posted. Did you trim the exception in an attempt to sensationalize the problem?

But who cares about the exception message python returned when there are /so/ many abominations in this code sample. I mean, i know this is an example, but it should not be an example of "how not to code"+"ANYTHING"+"!"*10

Some of the problems include:

1. You are using the print function (so we can assume you are using Python 3.x) but then you go and use that old ugly "%" string interpolation syntax crap! when you should have used the format method of strings.

    print("Item at index {0} is missing".format(index-1)) # Oops!
    
...Oh Steven, if you only knew how we interpreted the "Oops!", more like "Doh!".

2. Your stdout message is not only confusing, it's a freaking lie! 

    "Item at index %d is missing". 
    
...Steven, i can assure you that if "list[index]" raises an IndexError, the item is NOT /missing/. Neither the item OR the index even /exist/ as far as the sequence is concerned. Contrary to your naive beliefs, Python sequences are not initialized with every possible integer index (that the current machine can represent) just sitting around twiddling their thumbs complaining of boredom whilst waiting for a value to come along they can point to; can you imagine the memory usage of such a design (flaw)?

3. Your claim that the broken code in the "exception block" masks the exception that would have been raised by the code in the "try block" is false, because if it's true, then you'd better find the fool who told Python to mask the try block in the first place!

Got any more bright ideas DeAprano? (Oh gawd that felt good!)

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


Thread

Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-12 06:15 +0000
  Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-12 10:13 -0500
  Re: Awsome Python - chained exceptions Zero Piraeus <schesis@gmail.com> - 2013-02-12 14:01 -0400
    Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 19:01 -0800
      Re: Awsome Python - chained exceptions Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-14 00:00 -0700
        Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-14 16:10 -0800
    Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 19:01 -0800
  Re: Awsome Python - chained exceptions Ethan Furman <ethan@stoneleaf.us> - 2013-02-12 10:15 -0800
  Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 18:47 -0800
    Re: Awsome Python - chained exceptions Michael Torrie <torriem@gmail.com> - 2013-02-12 21:18 -0700
    Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-13 17:58 +1100
      Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:14 -0800
        Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:19 -0800
        Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:19 -0800
        Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-14 09:10 +1100
          Re: Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-14 06:39 +0000
            Re: Awsome Python - chained exceptions Serhiy Storchaka <storchaka@gmail.com> - 2013-02-15 20:51 +0200
        Re: Awsome Python - chained exceptions Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-14 13:01 +0100
          Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 18:56 -0800
            Re: Awsome Python - chained exceptions Chris Angelico <rosuav@gmail.com> - 2013-02-15 17:18 +1100
              Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 23:51 -0800
                Re: Awsome Python - chained exceptions Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-15 10:00 +0100
              Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 23:51 -0800
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-17 17:35 -0800
                Re: Awsome Python - chained exceptions Dave Angel <davea@davea.name> - 2013-02-17 20:48 -0500
                Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-17 21:51 -0800
                news.gmane.org (was Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-18 01:10 -0500
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-18 09:12 -0800
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-18 18:32 +0000
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Terry Reedy <tjreedy@udel.edu> - 2013-02-18 17:45 -0500
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-18 06:30 +0000
                Re: news.gmane.org (was Re: Awsome Python - chained exceptions Kwpolska <kwpolska@gmail.com> - 2013-02-18 16:27 +0100
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-18 18:18 -0800
                Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-19 07:52 -0800
                Re: Awsome Python - chained exceptions rusi <rustompmody@gmail.com> - 2013-02-19 09:14 -0800
                Re: Awsome Python - chained exceptions alex23 <wuwei23@gmail.com> - 2013-02-19 22:57 -0800
                Re: Awsome Python - chained exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:50 +1100
                Re: Awsome Python - chained exceptions Rotwang <sg552@hotmail.co.uk> - 2013-02-20 16:00 +0000
                RE: Awsome Python - chained exceptions "J. Marc Edwards" <marc.edwards@nimbisservices.com> - 2013-02-20 11:08 -0500
                Re: Awsome Python - chained exceptions rurpy@yahoo.com - 2013-02-20 08:13 -0800
                Re: Awsome Python - chained exceptions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-20 18:49 -0500
                Re: Awsome Python - chained exceptions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-20 18:47 -0500
      Re: Awsome Python - chained exceptions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:14 -0800
    Re: Awsome Python - chained exceptions Michael Torrie <torriem@gmail.com> - 2013-02-13 00:22 -0700

csiph-web