Exceptions

D

DavidPorterHolt

Can anyone explain why the second print statement below (copied from
'Dive Into Python') generates a syntax error? As I understand
exceptions, the except block should handle the exception, and the
following print statement should execute normally.
fsock = open('/nothere')
except IOError:
print 'The file does not exist, exiting gracefully'
print 'This line will always print'
SyntaxError: invalid syntax
Any help will be much appreciated.

Thanks,
David Holt
(e-mail address removed)
 
D

DavidPorterHolt

Here's the same clip, but run in the command line interpreter. The
error gives a little more info.
.... fsock = open('/notthere')
.... except IOError:
.... print 'The file does not exist, exiting gracefully'
.... print 'This line will always print'
File "<stdin>", line 5
print 'This line will always print'
^
SyntaxError: invalid syntax
Thanks,
David Holt
(e-mail address removed)
 
P

Paul McGuire

DavidPorterHolt said:
Here's the same clip, but run in the command line interpreter. The
error gives a little more info.

... fsock = open('/notthere')
... except IOError:
... print 'The file does not exist, exiting gracefully'
... print 'This line will always print'
File "<stdin>", line 5
print 'This line will always print'
^
SyntaxError: invalid syntax

Thanks,
David Holt
(e-mail address removed)
In the interpreter, after an indented group (such as your except IOError
block), you have to enter a blank line, THEN enter the 2nd print statement:
.... fsock = open('/notthere')
.... except IOError:
.... print 'The file does not exist, exiting gracefully'
....
The file does not exist, exiting gracefully
(Note the empty ellipsis, indicating a line with no more indention.)

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top