Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: How to handle exceptions properly in a pythonic way? Date: Thu, 5 Nov 2015 15:51:41 +1100 Lines: 46 Message-ID: References: <4b303213-62e2-42d4-b2f6-4fc1f6025944@googlegroups.com> <4cb51da8-9331-4efe-87d3-dfe9c2ae0f8e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de afMl3fyebMR273jwEiGtxAsRCHdat8c/a1lfERt/CAwg== 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; 'exception.': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'covered.': 0.09; 'lost.': 0.09; 'retained': 0.09; 'unexpected': 0.09; 'unhandled': 0.09; 'whatever.': 0.09; 'bug': 0.10; 'exception': 0.13; 'def': 0.13; 'thu,': 0.15; 'correctly,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'omitting': 0.16; 'prefer?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'summarizes': 0.16; 'threw': 0.16; 'try/except': 0.16; 'unambiguous': 0.16; 'wrote:': 0.16; 'result,': 0.18; 'try:': 0.18; 'runs': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exceptions': 0.22; 'occurs': 0.22; 'code,': 0.23; 'decide': 0.23; 'somewhere': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'wraps': 0.29; 'raise': 0.29; 'too.': 0.30; 'becomes': 0.30; "i'd": 0.31; "can't": 0.32; 'problem': 0.33; 'traceback': 0.33; 'case,': 0.34; 'structure': 0.34; 'except': 0.34; 'server': 0.34; 'info': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'something': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'wanted': 0.37; 'received:209': 0.38; 'anything': 0.38; 'means': 0.39; 'data': 0.39; "didn't": 0.39; 'ever': 0.60; 'your': 0.60; 'back': 0.62; 'information': 0.63; 'managing': 0.66; 'else.': 0.66; 'useful.': 0.72; 'chrisa': 0.84; 'happened.': 0.84; 'subject:handle': 0.84; 'working,': 0.84; 'dealt': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=OWdHvNgpTqZ6BpMcOUeMiWdUux+FG69y97ncS55A0lA=; b=lNa+l/glZOE0tW5hIxzjYJ6OVCa3rfWLwg2FR6tlCNhjiXXdsqy5i7YUqFcKM/UAPK GhIT/F8wsvRFeOFAr6k3af5wh63US00lYXhHXIpj+ubgiQCteK7KU1DsSV4Xymk9u/D1 GsPtnj7ys7hEzclWogD6jRnV5qGHTUDMjds3T9tIVrvcE3WBMVuShWKh+NYJb8gaRUlb 00QJSKNLZ3Fi8sHQQJBDDfQztJgrSOi/gL7sCL7VJcrzw/3xXgQ76RSi0nBBb+4hQaH8 MNa5s1G6NGJ7K5w1fRUwXn/RrxOwTU05hI0mJiOtsHDwQ7KAsWd+YsT0HEa2Bc7CkGti l8Dw== X-Received: by 10.50.41.74 with SMTP id d10mr918198igl.94.1446699102011; Wed, 04 Nov 2015 20:51:42 -0800 (PST) In-Reply-To: <4cb51da8-9331-4efe-87d3-dfe9c2ae0f8e@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98275 On Thu, Nov 5, 2015 at 3:18 PM, wrote: >> Which would you prefer? > > So if I am just checking for the ConnectionError in get_html and a new exception arises, I will have traceback to the get_html function showing that unhandled exception has happened. > Now I have to put additional exception block for managing the new exception in the get_html function and I am covered. > > Is that what you wanted to say? Yep. I would definitely recommend having get_html raise an exception on any problem; here's the structure I'd use: def get_html(...): try: ... actually go get the info return info except (ConnectionError, OSError, SocketError) as e: raise ContentNotFoundError from e This means there are three possibilities: 1) Everything works correctly, and get_html returns something useful. 2) An exception occurs of a type that you expect - the server didn't respond, or the OS threw back a failure, or whatever. (You'd decide what set of exceptions this represents.) In this case, you get a single exception that wraps that up and summarizes the problem as "Content Not Found". The original exception is retained in the __cause__ of the ContentNotFoundError, so no information is lost. 3) An unexpected exception occurs - your script runs out of memory, or there's a bug in your code, or anything else. The exception will simply bubble up, and be dealt with somewhere else. To use this code, what you'd do is: try: data = get_html(...) except ContentNotFoundError: # deal with the case of not having anything to work with Omitting the try/except is a perfectly reasonable way of working, too. Just call get_html, and if ever it can't get a result, the exception continues to propagate. It's easy, it's straight-forward, and it's unambiguous - any problem you don't expect becomes an exception. ChrisA