Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.01; 'anticipate': 0.07; 'type,': 0.07; 'urllib2': 0.07; 'python': 0.08; 'either.': 0.09; 'namespace': 0.09; 'subject:error': 0.10; 'better:': 0.16; 'httperror:': 0.16; 'occur.': 0.16; 'wrote:': 0.16; '>>>': 0.18; "haven't": 0.20; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'correctly.': 0.24; 'specify': 0.24; 'do,': 0.25; 'expect': 0.25; 'code': 0.25; 'tried': 0.26; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; "won't": 0.29; 'print': 0.29; 'recognized': 0.30; 'imported': 0.30; 'situations': 0.30; "didn't": 0.31; 'error': 0.32; 'does': 0.32; 'anyone': 0.32; "what's": 0.33; "can't": 0.33; 'there': 0.33; 'to:addr:python- list': 0.33; 'difference': 0.34; 'recognize': 0.34; 'try:': 0.34; 'thank': 0.35; 'certain': 0.35; 'skip:" 10': 0.36; 'example,': 0.37; 'statements': 0.37; 'response': 0.37; 'using': 0.37; 'put': 0.37; 'but': 0.37; 'two': 0.37; 'think': 0.38; 'too,': 0.38; 'received:google.com': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.39; 'might': 0.40; "it's": 0.40; 'where': 0.40; 'more': 0.60; 'your': 0.61; 'you.': 0.62; 'cause': 0.67; 'august': 0.70; 'subject:... ': 0.84; 'subject:types': 0.84 MIME-Version: 1.0 Sender: z@etiol.net X-Originating-IP: [190.52.50.194] In-Reply-To: <58958843-AE74-44BB-B5E3-96D7A37D779E@mssm.edu> References: <58958843-AE74-44BB-B5E3-96D7A37D779E@mssm.edu> Date: Fri, 19 Aug 2011 15:30:55 -0400 X-Google-Sender-Auth: sY83ZmmudsmCDbLxAZvJ2UEduJQ Subject: Re: try... except with unknown error types From: Zero Piraeus To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313782258 news.xs4all.nl 23965 [2001:888:2000:d::a6]:60321 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11868 : On 19 August 2011 15:09, Yingjie Lin wrote: > > I have been using try...except statements in the situations where I can expect a certain type of errors might occur. > But sometimes I don't exactly know the possible error types, or sometimes I just can't "spell" the error types correctly. > For example, > > try: > response = urlopen(urljoin(uri1, uri2)) > except urllib2.HTTPError: > print "URL does not exist!" > > Though "urllib2.HTTPError" is the error type reported by Python, Python doesn't recognize it as an error type name. > I tried using "HTTPError" alone too, but that's not recognized either. > > Does anyone know what error type I should put after the except statement? or even better: is there a way not to specify > the error types? Thank you. You should always specify the error type, so that your error-handling code won't attempt to handle an error it didn't anticipate and cause even more problems. In this case, I think it's just that you haven't imported HTTPError into your namespace - if you do, it works: >>> from urllib2 import urlopen, HTTPError >>> try: ... response = urlopen("http://google.com/invalid") ... except HTTPError: ... print "URL does not exist!" ... URL does not exist! >>> Alternatively: >>> import urllib2 >>> try: ... response = urllib2.urlopen("http://google.com/invalid") ... except urllib2.HTTPError: ... print "URL does not exist!" ... URL does not exist! >>> A careful look at the difference between these two ought to make it clear what's going on. -[]z.