Exceptions
Paul McGuire
ptmcg at austin.rr._bogus_.com
Tue Feb 21 12:05:39 EST 2006
"DavidPorterHolt" <DavidPorterHolt at acm.org> wrote in message
news:1140540525.032755.209460 at g47g2000cwa.googlegroups.com...
> Here's the same clip, but run in the command line interpreter. The
> error gives a little more info.
>
> >>> try:
> ... 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
> DavidPorterHolt at acm.org
>
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:
>>> try:
... fsock = open('/notthere')
... except IOError:
... print 'The file does not exist, exiting gracefully'
...
The file does not exist, exiting gracefully
>>> print 'This line will always print'
This line will always print
>>>
(Note the empty ellipsis, indicating a line with no more indention.)
-- Paul
More information about the Python-list
mailing list