[Tutor] Exception handling - syntaxerror?!

Krishna calvinkrishy at gmail.com
Mon Sep 26 07:20:14 CEST 2005


On 25/09/05, Andrei <project5 at redrival.net> wrote:
> > Think the mail system screwed up the formatting! But am fairly sure
> > that I have indented it correctly in the console. Try and Except are
> > in the column. Any other hints?
>
> Yes :). Compare:
>
>  >>> try:
> ...   os.system('cls')
> ... except:
> ...   print "Foo"
> ...
> Foo
>  >>> print "Bar"
> Bar
>
> With (what you had):
>
>  >>> try:
> ...   os.system('cls')
> ... except:
> ...   print "Foo"
> ... print "Bar"
>    File "<stdin>", line 5
>      print "Bar"
>          ^
> SyntaxError: invalid syntax
>
> Subtle, but important difference. You should terminate the try-except
> block (confirm with extra ENTER) before doing more stuff.

When I have this piece of code in a function, it works fine. So is
this some limitation of directly working out off the console?

Seems the console supports complete code constructs only  I.e., I can
have one complete try...except block, a if block etc., but not a bunch
of constructs? Is my assumption correct?

Thanks.

>
> --
> Yours,
>
> Andrei


More information about the Tutor mailing list