[Python-Dev] FW: Bare except clauses in PEP 348

2005-08-24 Thread Raymond Hettinger
Hey guys, don't give up your bare except clauses so easily.

They are useful.  And, if given the natural meaning of catch
everything and put in a natural position at the end of a suite, their
meaning is plain and obvious.  Remember beauty counts.  I don't think
there would be similar temptation to eliminate a dangling else clause
and replace it with else Everything.  Nor would a final default case
in a switch statement benefit from being written as default
Everything.

The thought is that it is okay to have useful defaults.  My whole issue
was that the PEP was choosing the wrong default.  If we leave it alone,
all is well.  An empty except will continue to mean catch everything,
it will always appear at the end, its meaning will be obvious, and
existing working code won't break :-)

On the occasions where you really intended to catch everything, do you
really want to go on an editing binge just to uglify the code to
something like:

   try:   
   ...
   except SomeException: 
   ...
   except BaseException:  
   ...


It is more beautiful and clear as:

   try:   
   ...
   except SomeException: 
   ...
   except:  
   ...

To me, the latter is more attractive and is more obviously a catchall,
just like an else-clause or a default-statement.  It is a strong visual
cue that at least one of the except clauses will always be triggered.
In contrast, the first example makes you think twice about whether the
final case really does get everything (sometimes implicit IS better than
explicit).



Raymond

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] FW: Bare except clauses in PEP 348

2005-08-24 Thread Michael Chermside
Raymond writes:
 Hey guys, don't give up your bare except clauses so easily.
  [...]

Raymond:

I agree that when comparing:

   // Code snippet A
   try:
   ...
   except SomeException:
   ...
   except BaseException:
   ...

with

   // Code snippet B
   try:
   ...
   except SomeException:
   ...
   except:
   ...

that B is nicer than A. Slightly nicer. It's a minor esthetic point. But
consider these:

// Code snippet C
try:
...
except Exception:
...

// Code snippet D
try:
...
except:
...

Esthetically I'd say that D is nicer than A for the same reasons. It's a minor
esthetic point. But you see, this case is different. You and I would likely
never bother to compare C and D because they do different things! (D is
equivalent to catching BaseException, not Exception). But we know that people
who are not so careful or not so knowlegable WILL make this mistake... they
make it all the time today!

Since situation C (catching an exception) is hundreds of times more common than
situation A (needing default processing for exceptions that don't get caught,
but doing it with try-except instead of try-finally because the
nothing-was-thrown case is different), I would FAR rather protect beginners
from erroniously confusing C and D than I would provide a marginally more
elegent syntax for the experts using A or B. And that elegence is arguable...
there's something to be said for simplicity, and having only one kind of
except clause for try statements is clearly simpler than having both except
some-exception-type: and also bare except:.

-- Michael Chermside

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] FW: Bare except clauses in PEP 348

2005-08-24 Thread Ron Adam
Raymond Hettinger wrote:
  Hey guys, don't give up your bare except clauses so easily.

Yes, Don't give up.  I often write code starting with a bare except, 
then after it works, stick a raise in it to determine exactly what 
exception I'm catching. Then use that to rewrite a more explicit except 
statement.

Your comment earlier about treating the symptom is also accurate.  This 
isn't just an issue with bare excepts not being allowed in the middle, 
it also comes up whenever we catch exceptions out of order in the tree. 
  Ie.. catching an exception closer to the base will block a following 
except clause that tries to catch an exception on the same branch.

So should except clauses be checked for orderliness?

Regards, Ron

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] FW: Bare except clauses in PEP 348

2005-08-24 Thread Guido van Rossum
On 8/24/05, Raymond Hettinger [EMAIL PROTECTED] wrote:
 Hey guys, don't give up your bare except clauses so easily.

They are an attractive nuisance by being so much shorter to type than
the right thing to do.

Especially if they default to something whose use cases are rather
esoteric (i.e. BaseException).

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com