Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > I don't get why sys.exit(1) doesn't exit the while loop in the follow case

Reply
Thread Tools

I don't get why sys.exit(1) doesn't exit the while loop in the follow case

 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-11-2010
In message <(E-Mail Removed)>, Nobody wrote:

> If I'm catching exceptions in order to perform clean-up, I'll use a bare
> except and re-raise the exception afterwards. In that situation, a bare
> except is usually the right thing to do.


Wrong way to do it.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-11-2010
Steven D'Aprano wrote:

> On Tue, 05 Oct 2010 13:57:11 +1100, Ben Finney wrote:
>
>> chad <(E-Mail Removed)> writes:
>>
>>> while 1:

>>
>> A minor point: this is more explanatory and less misleading if you write
>> it as ‘while True’.

>
> Why is it misleading? Is there some circumstance in Python where the
> literal 1 could have a false value?


Wrong question.

>>> while True:

.... print "forever"
....
>>> True

0

So, if I were to play advocatus diaboli I'd argue that 'while True: ...' is
more likely to mislead because 'True' could be anything.

Peter

PS: The above is no longer possible in Python 3 where True and False have
become keywords.
 
Reply With Quote
 
 
 
 
Ethan Furman
Guest
Posts: n/a
 
      10-11-2010
Lawrence D'Oliveiro wrote:
> In message <(E-Mail Removed)>, Nobody wrote:
>
>>If I'm catching exceptions in order to perform clean-up, I'll use a bare
>>except and re-raise the exception afterwards. In that situation, a bare
>>except is usually the right thing to do.

>
> Wrong way to do it.


What, then, is the right way to do it?

~Ethan~
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      10-12-2010
On Mon, 11 Oct 2010 05:42:39 -0700, Ethan Furman wrote:

>>>If I'm catching exceptions in order to perform clean-up, I'll use a bare
>>>except and re-raise the exception afterwards. In that situation, a bare
>>>except is usually the right thing to do.

>>
>> Wrong way to do it.

>
> What, then, is the right way to do it?


I presume that he's referring to "finally". Which is reasonable enough
given what I wrote, but isn't always convenient.

My point was that bare excepts aren't a problem if you're going to
re-raise the exception.

 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      10-13-2010
Nobody <(E-Mail Removed)> writes:

> On Mon, 11 Oct 2010 05:42:39 -0700, Ethan Furman wrote:
>
>>>>If I'm catching exceptions in order to perform clean-up, I'll use a bare
>>>>except and re-raise the exception afterwards. In that situation, a bare
>>>>except is usually the right thing to do.
>>>
>>> Wrong way to do it.

>>
>> What, then, is the right way to do it?

>
> I presume that he's referring to "finally". Which is reasonable enough
> given what I wrote, but isn't always convenient.
>
> My point was that bare excepts aren't a problem if you're going to
> re-raise the exception.


If I understand correctly, there is a big difference between bare except
and finally:

>>> def f(x):

.... try:
.... if x:
.... raise ValueError("x should be falsy")
.... except:
.... print("bare except")
.... raise
.... finally:
.... print("finally")
....
>>> f(0)

finally
>>> f(1)

bare except
finally
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in f
ValueError: x should be falsy

The finally: clause is always executed, whereas the bare except: clause
is only executed if an exception was raised in the try: clause.

--
Arnaud
 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Can I get the exit code "n" passed to sys.exit(n) ? Yujo Python 2 04-10-2007 08:35 PM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments