Exception handling for socket.error in Python 3.5/RStudio

dieter dieter at handshake.de
Sun Feb 7 03:23:04 EST 2016


Shaunak Bangale <shaunak.bangale at gmail.com> writes:
> ...
> while (count > 0):
>     try :
>         # read line from file:
>         print(file.readline())
>         # parse
>         parse_json(file.readline())
>         count = count - 1
>     except socket.error as e:
>         print('Connection fail', e)
>         print(traceback.format_exc())
> ...
> Error:
> except socket.error as e:
>                          ^
> SyntaxError: invalid syntax

Are you sure, that there is no invisible character at the end
of that line?

When I try code like the above (in PYthon 2.7), there is no
"SyntaxError":

>>> while(False):
...     try :
...         # read line from file:
...         print(file.readline())
...         # parse
...         parse_json(file.readline())
...         count = count - 1
...     except socket.error as e:
...         print('Connection fail', e)
...         print(traceback.format_exc())
... 
>>> 


Another reason for your problem could be an older Python version.
The "as" construct as part of the "except" clause is a more recent
addition to Python.



More information about the Python-list mailing list