Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; '21,': 0.07; 'lawrence': 0.09; 'subject:method': 0.09; 'try:': 0.09; 'chained': 0.16; 'connect.': 0.16; 'corrupt': 0.16; 'oserror': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'case.': 0.24; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'too.': 0.31; 'raised': 0.31; 'reduced': 0.31; 'file': 0.32; 'probably': 0.32; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'being': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'catch': 0.60; 'ian': 0.60; 'chance': 0.65; 'due': 0.66; '2015': 0.84; 'otten': 0.84; 'subject:try': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=pwy3TLRYN3jBPSc5gOdnM7hDtTBqe4PXmBd99oHSjh0=; b=Q+wYIFYZtyUQUTXj6+r0sfjK3NmcMOCWfq3S/p1ANvmgjnzACMCJSNPVfZfaAlpOJ0 d4icRoda6wa9Vnzev75ZJZAcTAWCnz73THHR1eHNykzKvhNXd2cxy+35Vwdduv0Jeiz5 q6O98bubsWxeTz4S6nQu/bWR6bR+HwlxX/ToerbrZuQRkBjOzo4O/F64slDwheH+BbKl 2aOEuUb2z21bFpPXtzS5QKOTnt2ZEgkPM3kuq3Tfl6zOhzD+S0Hz2o6jxtYtP7KFPPIt 8UADnXBOM/3AtHv1JVhOMXuKBV8CZ/1/zFQ7eQ8uaScDO9m9MztLK+VRUpZlaQ79eupx ehWw== X-Received: by 10.70.135.165 with SMTP id pt5mr4990854pdb.101.1424533848982; Sat, 21 Feb 2015 07:50:48 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> From: Ian Kelly Date: Sat, 21 Feb 2015 08:50:08 -0700 Subject: Re: try pattern for database connection with the close method To: Python Content-Type: text/plain; charset=UTF-8 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424533852 news.xs4all.nl 2844 [2001:888:2000:d::a6]:36614 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86050 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.