Re: "Try:" which only encompasses head of compound statement

2007-08-28 Thread Ben Finney
[EMAIL PROTECTED] writes: > In real life, I want to set some return values to sensible defaults > if the file doesn't exist, but I want the errors from inside the > block to trickle up as normal. Sure. Just be careful only to catch the exceptions that you do want to handle at that level of the co

Re: "Try:" which only encompasses head of compound statement

2007-08-28 Thread Jameson . Quinn
Those are some good responses, but I think they focused a little too much on the specifics of my example - especially the 'print' statement. In real life, I want to set some return values to sensible defaults if the file doesn't exist, but I want the errors from inside the block to trickle up as no

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Ben Finney
Scott David Daniels <[EMAIL PROTECTED]> writes: > Actually, this will be followed by some foolishness because > (1) [the "print" format argument was wrong] > and > (2) [the code attempts to loop over the failed input file anyway] You're right. Shame on me for posting untested code snippets, Thank

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Carl Banks wrote: > Consider this: is there any other code in your program that has to do > something different based on whether you successfully opened this file > or not? If so, how will you notify it whether the call has succeeded > or not? Very often, the call

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Scott David Daniels
Ben Finney wrote: > [EMAIL PROTECTED] writes: > ... try to only catch exceptions from the > minimum amount of code that does one discrete action. > > try: > input_file = open(my_filename) > except IOError, exc: > print "Can't open myfile: %(exc)" % locals() > > for lin

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Ben Finney
[EMAIL PROTECTED] writes: > try: > for line in open(myFileName): > count += openAndProcessSubfile(line) > except IOError: > print "Can't open myfile" > > ... now the 'except' incorrectly catches errors from > openAndProcessSubfile. So don't include it. You've discovered a good pr

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Carl Banks
On Aug 27, 5:35 pm, [EMAIL PROTECTED] wrote: > I have: > > try: > for line in open(myFileName): > count += 1 > except IOError: > print "Can't open myfile" > > (I know, this is bad, I never close the file, but its just for > illustration). But then I change it to: > > try: > for

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > try: > for line in open(myFileName): > count += 1 > except IOError: > print "Can't open myfile" > > (I know, this is bad, I never close the file, but its just for > illustration). No, that's fine. The file object returne

Re: "Try:" which only encompasses head of compound statement

2007-08-27 Thread Bjoern Schliessmann
[EMAIL PROTECTED] wrote: > I have: > > try: > for line in open(myFileName): > count += 1 > except IOError: > print "Can't open myfile" > > (I know, this is bad, I never close the file, but its just for > illustration). But then I change it to: > > try: > for line in open(myF

"Try:" which only encompasses head of compound statement

2007-08-27 Thread Jameson . Quinn
I have: try: for line in open(myFileName): count += 1 except IOError: print "Can't open myfile" (I know, this is bad, I never close the file, but its just for illustration). But then I change it to: try: for line in open(myFileName): count += openAndProcessSubfile(lin