Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '21,': 0.07; 'matches': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'try:': 0.09; 'random': 0.14; 'chained': 0.16; 'connect.': 0.16; 'corrupt': 0.16; 'op.': 0.16; 'oserror': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'bytes': 0.24; 'case.': 0.24; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'raise': 0.29; 'too.': 0.31; '"",': 0.31; '>>>>': 0.31; 'raised': 0.31; 'reduced': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'table': 0.34; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'being': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'catch': 0.60; 'ian': 0.60; 'chance': 0.65; 'due': 0.66; '2015': 0.84; 'otten': 0.84; 'subject:try': 0.84; 'encrypted': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: try pattern for database connection with the close method Date: Sat, 21 Feb 2015 18:02:50 +0100 Organization: None References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9545.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424538179 news.xs4all.nl 2959 [2001:888:2000:d::a6]:58233 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4496 X-Received-Body-CRC: 864001431 Xref: csiph.com comp.lang.python:86054 Ian Kelly wrote: > On Sat, Feb 21, 2015 at 8:27 AM, Peter Otten <__peter__@web.de> wrote: >> Ian Kelly wrote: >> >>> On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence >>> wrote: >>>> try: >>>> with lite.connect('data.db') as db: >>>> try: >>>> db.execute(sql, parms) >>>> except lite.IntegrityError: >>>> raise ValueError('invalid data') >>>> except lite.DatabaseError: >>>> raise OSError('database file corrupt or not found.') >>> >>> This could result in the OSError being misleadingly raised due to some >>> DatabaseError raised by the execute rather than the connect. >> >> The OP probably wants to catch these DatabaseErrors, too. Also, the >> chance of a misleading traceback has been greatly reduced with the advent >> of chained exceptions. >> > > Yes, but the point is that OSError is probably inappropriate in that case. Perhaps, but the example I gave in my other post: >>> with open("data.db", "wb") as f: ... f.write(os.urandom(1024)) # put random bytes into data.db >>> db = sqlite3.connect("data.db") >>> db.execute("create table foo (bar, baz);") Traceback (most recent call last): File "", line 1, in sqlite3.DatabaseError: file is encrypted or is not a database matches the "database file corrupt" part of the error message provided by the OP.