Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail 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; 'anyway.': 0.04; 'except:': 0.07; 'exception.': 0.07; 'stops': 0.07; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'exception': 0.13; 'wed,': 0.15; '"connection': 0.16; "'connection": 0.16; 'control-c': 0.16; 'doesnt': 0.16; 'error"': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'hangs': 0.16; 'message- id:@fido.openend.se': 0.16; 'pairs,': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'subject:handling': 0.16; 'laura': 0.18; 'try:': 0.18; '2015': 0.20; 'do.': 0.22; 'exceptions': 0.22; 'resumes': 0.22; 'see:': 0.22; 'sep': 0.22; 'subject:problem': 0.22; 'trying': 0.22; 'script': 0.25; "doesn't": 0.26; 'error': 0.27; '-0700,': 0.29; 'catching': 0.29; 'received:se': 0.29; 'terminating': 0.29; 'asked': 0.29; 'print': 0.30; 'connection': 0.30; 'url:python': 0.33; 'raised': 0.33; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'charset :us-ascii': 0.37; 'mean': 0.38; 'end': 0.39; 'why': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'press': 0.61; 'header:Message-Id:1': 0.61; 'telling': 0.61; 'above,': 0.63; '>if': 0.84; 'header:In-reply-to:1': 0.84; 'absolutely': 0.88 To: python-list@python.org From: Laura Creighton Subject: Re: ConnectionError handling problem In-reply-to: References: Comments: In-reply-to shiva upreti message dated "Wed, 23 Sep 2015 19:49:17 -0700." MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13213.1443091109.1@fido> Content-Transfer-Encoding: quoted-printable Date: Thu, 24 Sep 2015 12:38:29 +0200 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Thu, 24 Sep 2015 12:38:31 +0200 (CEST) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443091118 news.xs4all.nl 23830 [2001:888:2000:d::a6]:43204 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97068 In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes: >Hi >If my script hangs because of the reasons you mentioned above, why doesnt= it catch ConnectionError? >My script stops for a while and when I press CTRL+C, it shows ConnectionE= rror without terminating the process, and the script resumes from where it= left off. This is exactly what you asked it to do. :) >> try: >> r=3Drequests.post(url, data=3Dquery_args) >> except: >> print "Connection error" >> time.sleep(30) >> continue try to do something until you get an Exception. Since that is a naked except, absolutely any Exception will do. That you print out 'Connection error' doesn't mean that you are only catching exceptions raised by trying to send something to the other end ... any Exception will do. So what happens when you press Control-C? You get a KeyboardInterrupt exception! :) see: https://docs.python.org/2/library/exceptions.html And you are catching it :) And when you catch it you print Connection error and keep on trying. This is why people were telling you that naked try:except: pairs, are rarely what you want. You didn't want to catch control-c but you caught it anyway. Laura