Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11869

Re: try... except with unknown error types

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <Yingjie.Lin@mssm.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'anticipate': 0.07; 'type,': 0.07; 'urllib2': 0.07; 'python': 0.08; '>>>>': 0.09; 'either.': 0.09; 'header:In-reply-to:1': 0.09; 'namespace': 0.09; 'subject:error': 0.10; 'better:': 0.16; 'httperror:': 0.16; 'occur.': 0.16; 'piraeus': 0.16; 'to:addr:schesis': 0.16; 'to:name:zero piraeus': 0.16; 'x-mailer:apple mail (2.1084)': 0.16; 'zero,': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; "haven't": 0.20; "doesn't": 0.22; 'cc:2**0': 0.22; 'pm,': 0.24; 'aug': 0.24; 'correctly.': 0.24; 'specify': 0.24; 'do,': 0.25; 'expect': 0.25; 'code': 0.25; 'tried': 0.26; 'import': 0.28; "won't": 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; '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; 'difference': 0.34; '...': 0.34; 'recognize': 0.34; 'try:': 0.34; 'thank': 0.35; 'certain': 0.35; 'charset:us-ascii': 0.36; '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; 'received:org': 0.38; 'too,': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'subject:with': 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
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mssm.edu; h=date : from : subject : in-reply-to : to : cc : message-id : mime-version : content-type : content-transfer-encoding : references; s=mail; bh=L7cGGaXPAg298PjYoNs+k20egrjMIgDAi/xFNECbqro=; b=iULFsb8amDPs6psMSE7ZABEz6H8G3g4M8YlXi7HIJ8doPfs2OR0NGhLRR9Z2Oq/TUCBo WfEUyPbQdQkmyLL4C/p3dq33fbWL4MGLht5bcYZTvW6mjAYMmf4CbuED43HNiQ89igM4 go8SQ/tR1OigAUokTomNlhj1/01xbA66/CD4zzvUWAIZQUoBZTklAu2P+BwlgFv3h3Bu GJJ0ds5Z7W1RPQk2Qzenya5yqk4supIaotxxsYCNTJyIaa9J0X9OPB4mvzQ1FSXNi+6p 8MM46X1EdQVQsBjkwE2k00ghHwU31qlieZoNg9jH6GvwoXSNJLpoSQdNrodqbGVUI67X cg==
Date Fri, 19 Aug 2011 15:35:56 -0400
From Yingjie Lin <Yingjie.Lin@mssm.edu>
Subject Re: try... except with unknown error types
In-reply-to <CANaSqUfEO45ZE098VrFuOp1=bE1_Bcdk0wBtV2gP8AWNMcdbvA@mail.gmail.com>
To Zero Piraeus <schesis@gmail.com>
MIME-version 1.0
X-Mailer Apple Mail (2.1084)
Content-type text/plain; charset=us-ascii
Content-transfer-encoding 7BIT
References <58958843-AE74-44BB-B5E3-96D7A37D779E@mssm.edu> <CANaSqUfEO45ZE098VrFuOp1=bE1_Bcdk0wBtV2gP8AWNMcdbvA@mail.gmail.com>
X-Proofpoint-Virus-Version vendor=fsecure engine=2.50.10432:5.4.6813, 1.0.211, 0.0.0000 definitions=2011-08-19_07:2011-08-19, 2011-08-19, 1970-01-01 signatures=0
X-Proofpoint-Spam-Details rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=6.0.2-1012030000 definitions=main-1108190217
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.234.1313782560.27778.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313782560 news.xs4all.nl 23870 [2001:888:2000:d::a6]:39930
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11869

Show key headers only | View raw


Hi Zero,

I see! This is very helpful. Thank you.

- Yingjie







On Aug 19, 2011, at 3:30 PM, Zero Piraeus wrote:

> :
> 
> On 19 August 2011 15:09, Yingjie Lin <Yingjie.Lin@mssm.edu> 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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: try... except with unknown error types Yingjie Lin <Yingjie.Lin@mssm.edu> - 2011-08-19 15:35 -0400

csiph-web