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


Groups > comp.lang.python > #31242

Re: Understanding and dealing with an exception

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feed.xsnews.nl!border-2.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'except:': 0.07; 'exception.': 0.07; 'pil': 0.07; 'try:': 0.07; 'failed:': 0.09; 'ioerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; "wouldn't": 0.11; 'files.': 0.13; '.txt': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exception': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'test.': 0.17; 'file:': 0.22; 'example': 0.23; 'raise': 0.24; 'idea': 0.24; 'script': 0.24; 'specifically': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'instead.': 0.27; 'header:X-Complaints-To:1': 0.28; 'catching': 0.29; 'error': 0.30; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'from:addr:yahoo.co.uk': 0.32; 'to:addr:python-list': 0.33; 'doing': 0.35; 'received:org': 0.36; 'except': 0.36; "i'll": 0.36; 'subject:with': 0.36; 'test': 0.36; 'bad': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'files': 0.38; 'some': 0.38; 'advice': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; "you've": 0.61; 'identify': 0.61
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Understanding and dealing with an exception
Date Sun, 14 Oct 2012 09:20:37 +0100
References <CALyJZZXtxD5HZCZ6C21-uPgxQUhvWw7xaUZsmPw-whTvhjGBsA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-146-1-240.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:16.0) Gecko/20121005 Thunderbird/16.0
In-Reply-To <CALyJZZXtxD5HZCZ6C21-uPgxQUhvWw7xaUZsmPw-whTvhjGBsA@mail.gmail.com>
X-Antivirus avast! (VPS 121013-1, 13/10/2012), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2171.1350202826.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350202826 news.xs4all.nl 6962 [2001:888:2000:d::a6]:48935
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31242

Show key headers only | View raw


On 14/10/2012 05:23, Vincent Davis wrote:
> I am working on a script to find bad image files. I am using PIL
> and specifically image.verify() I have a set of known to be bad image files
> to test. I also what to be able to test any file for example a .txt and
> deal with the exception.
> Currently my code is basically
>
> try:
>      im = Image.open(ifile)
>      try:
>          print(im.verify())
>      except:
>          print('Pil image.verify() failed: ' + afile)
> except IOError:
>      print('PIL cannot identify image file: ' + afile)
> except:
>      print(ifile)
>      print("Unexpected error doing PIL.Image.open():", sys.exc_info()[0])
>      raise

[snip]

>
> Vincent
>

You've already had some advice so I'll just point out that a bare except 
is a bad idea as you wouldn't even be able to catch a user interrupt. 
Try (groan!) catching StandardError instead.

-- 
Cheers.

Mark Lawrence.

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


Thread

Re: Understanding and dealing with an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-14 09:20 +0100

csiph-web