Path: csiph.com!2.eu.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Exception handling for socket.error in Python 3.5/RStudio Date: Sun, 07 Feb 2016 09:23:04 +0100 Lines: 42 Message-ID: References: <1067e50c-bcbd-464e-8013-754bd66fda91@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de rdv/Vqtn1HUIc1LEbgSITQB3NPceS0+VvmgSOttclXdQ== Cancel-Lock: sha1:51blYbYPgIIqLQMvCndFXvHVoG0= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'error:': 0.05; 'clause': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'socket.error': 0.09; 'python': 0.10; 'python.': 0.11; 'syntax': 0.13; '0):': 0.16; 'invisible': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:handling': 0.16; 'syntaxerror:': 0.16; 'version.': 0.18; '(in': 0.18; '>>>': 0.20; 'file:': 0.22; 'parse': 0.22; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sure,': 0.29; 'character': 0.29; 'subject:/': 0.30; 'code': 0.30; 'another': 0.32; 'older': 0.32; 'problem': 0.33; 'except': 0.34; 'could': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'skip:p 20': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'more': 0.63; 'received:217': 0.66; 'construct': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pd9e09138.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102618 Shaunak Bangale 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.