try..except with empty exceptions

Cameron Simpson cs at zip.com.au
Sat Apr 11 05:27:27 EDT 2015


On 10Apr2015 19:38, Rustom Mody <rustompmody at gmail.com> wrote:
>On Saturday, April 11, 2015 at 7:53:31 AM UTC+5:30, Dave Angel wrote:
>> On 04/10/2015 09:42 PM, Steven D'Aprano wrote:
>> > On Sat, 11 Apr 2015 05:31 am, sohcahtoa82 wrote:
>> >> It isn't document because it is expected.  Why would the exception get
>> >> caught if you're not writing code to catch it?  If you write a function
>> >> and pass it a tuple of exceptions to catch, I'm not sure why you would
>> >> expect it to catch an exception not in the tuple.  Just because the tuple
>> >> is empty doesn't mean that it should catch *everything* instead.  That
>> >> would be counter-intuitive.
>> >
>> > Really? I have to say, I expected it.
>>
>> I'm astounded at your expectation.  That's like saying a for loop on an
>> empty list ought to loop on all possible objects in the universe.
>
>To work, this analogy should also have two python syntaxes like this:
>
>"Normal" for-loop:
>for var in iterable:
>  suite
>
>"Empty" for-loop:
>for:
>  suite

Well, to throw an anaolgy in the mix, the shell has a bare for syntax.

  list='a b c'
  for x in $list
  do  echo $x
  done

echoes a, b and c as you might expect.

This:

  for x
  do  echo $x
  done

echoes the command line arguments. (Which is extremely useful.)

But as with Python, the missing iteration means a _default_ source of 
iteration, not _no_ iteration.

To continue the analogy, this:

  list=
  for x in $list
  do  echo $x
  done

echoes nothing. As I would hope Steven would expect.

Importantly, in both Python and the shell you have a way to specify "nothing".  

If the empty tuple were to mean "catch everything" then there would not be a 
way to express "catch nothing". Bad bad bad!

Consider this a proof that Python's current meanings for bare except and 
"except ()" are sensible, using a proof by contradiction.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list