Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: else in try/except

Reply
Thread Tools

Re: else in try/except

 
 
Ethan Furman
Guest
Posts: n/a
 
      11-15-2011
Thanks, all!

~Ethan~
 
Reply With Quote
 
 
 
 
Barry W Brown
Guest
Posts: n/a
 
      11-15-2011
I thought that the point of the else clause is that it is reached only
if there is no exception in the try clause.
 
Reply With Quote
 
 
 
 
Barry W Brown
Guest
Posts: n/a
 
      11-15-2011
I thought that the point of the else clause is that it is reached only
if there is no exception in the try clause.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      11-15-2011
On 2011-11-15, Barry W Brown <> wrote:

> I thought that the point of the else clause is that it is reached
> only if there is no exception in the try clause.


Not really. If that's all you wanted, then you just put the code at
the end of the try block.

--
Grant Edwards grant.b.edwards Yow! ... I see TOILET
at SEATS ...
gmail.com
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      11-15-2011
On 11/15/11 2:31 PM, Grant Edwards wrote:
> On 2011-11-15, Barry W Brown<> wrote:
>
>> I thought that the point of the else clause is that it is reached
>> only if there is no exception in the try clause.

>
> Not really. If that's all you wanted, then you just put the code at
> the end of the try block.


No, he's right. You should only put code in the try: block where you want
exceptions to be caught. Everything else should be outside of the block. You
really do want to minimize the amount of code inside the try: block.

try:
# minimal code that might raise exceptions that you want to catch
except ThisError:
# handle ThisError exceptions, and probably continue execution
except ThatError:
# handle ThatError exceptions, and probably continue execution
else:
# Code that only runs if ThisError or ThatError were not
# raised in the try: block. This code may raise ThisError or ThatError
# exceptions that should not be caught by the above except: blocks.

# Other code that runs regardless if a caught-and-continued exception
# was raised or not

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
else if vs else { if A C++ 8 08-28-2010 08:55 PM
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
for: else: - any practical uses for the else clause? metaperl.etc@gmail.com Python 25 09-30-2006 11:01 PM
ruby idiom for python's for/else while/else Gergely Kontra Ruby 16 09-20-2005 08:35 PM
Wireless, what else? HelpPls Wireless Networking 2 07-14-2004 01:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57