Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'except:': 0.07; 'exception.': 0.07; 'appropriate.': 0.09; 'exception,': 0.09; 'exception:': 0.09; 'instance.': 0.09; 'cc:addr:python-list': 0.10; 'exception': 0.13; 'def': 0.14; 'instead.': 0.15; 'skip:f 30': 0.15; 'thu,': 0.15; '"from"': 0.16; 'bug,': 0.16; 'clause.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:send': 0.16; 'systemexit': 0.16; 'useless': 0.16; 'wrote:': 0.16; 'else,': 0.18; 'skip': 0.18; ';-)': 0.18; '>>>': 0.20; 'handling': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '"",': 0.22; 'terminate': 0.22; 'try:': 0.22; 'pass': 0.22; 'am,': 0.23; '2015': 0.23; 'slightly': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'raise': 0.24; 'sort': 0.25; 'message-id:@mail.gmail.com': 0.28; 'went': 0.28; 'division': 0.29; 'code:': 0.29; "i'd": 0.31; 'code': 0.31; 'up.': 0.32; 'class': 0.33; 'skip:_ 30': 0.33; 'traceback': 0.33; 'another': 0.34; 'case,': 0.34; 'file': 0.34; 'message.': 0.34; 'received:google.com': 0.34; 'something': 0.35; 'but': 0.36; 'except': 0.36; 'subject:: ': 0.37; 'skip:z 10': 0.38; 'test': 0.39; 'your': 0.60; 'avoid': 0.61; 'more': 0.62; 'complete': 0.63; 'different': 0.64; 'here': 0.66; 'direct': 0.70; 'useful.': 0.72; '(also,': 0.84; 'cecil': 0.84; 'chrisa': 0.84; 'westerhof': 0.84; 'to:none': 0.90 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:cc :content-type; bh=cJw68HvFmqXmDvlHsMkKq1qQmxITJRBeBMlCJ2/kzVI=; b=uI7eDIT+SfZaIRQzgemZMBVWVrVaxYn5BaFItOPHJG3zpERYYlNz/lR9R6xXgVXlvs EzVr2r4HuLNmnbBUSAjABBGKyE9LBgoG2c5J4UX/P0eusuQCQM12mAGDGnYwAPY896on ni3TNZEEKx5eFTW1Zbxxq1VCnE/Ilnq6ww5cgYl3Hb3n9/8ncI0O/SRnNWa/EdDOWfbx DPxf/+HtIoDgTIi7yMY73uQaH3Vi9Bcfkcvp1BqO85E6B181cCupInv5/H4pRX4M+vmY d/njRNkMW1BfvSupb1TvMw+km2aRLuEh99eZLiIsykE1BMZF70/5hNn/WS2bPRQVEMJS 78HQ== MIME-Version: 1.0 X-Received: by 10.107.131.196 with SMTP id n65mr6694ioi.53.1433368813110; Wed, 03 Jun 2015 15:00:13 -0700 (PDT) In-Reply-To: <876174ix9n.fsf@Equus.decebal.nl> References: <87egltht87.fsf@Equus.decebal.nl> <876174ix9n.fsf@Equus.decebal.nl> Date: Thu, 4 Jun 2015 08:00:13 +1000 Subject: Re: Retrying to send message From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433368816 news.xs4all.nl 2867 [2001:888:2000:d::a6]:42232 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92005 On Thu, Jun 4, 2015 at 2:15 AM, Cecil Westerhof wrote: >> And I'd also skip the bare except clause. If you get any sort of >> exception, whether it's a bug, a failure from libturpial, a network >> error, or anything else, your code will just terminate with a bland >> and useless message. Much better to simply let the exception bubble >> up. > > I kept the except. I like to see the message that went wrong. ;-) In that case, there's an easier way to deal with it: just raise a different exception, and let them chain. Something like this: >>> def send_message(msg): ... try: 1/0 ... except: raise FailedMessageException(msg) ... >>> class FailedMessageException(Exception): pass ... >>> send_message("Test") Traceback (most recent call last): File "", line 2, in send_message ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "", line 3, in send_message __main__.FailedMessageException: Test But I'd still avoid the bare except, and use "except Exception:" instead. I don't think you want to catch SystemExit in this way, for instance. So here's how I'd write that code: >>> def send_message(msg): ... try: 1/0 ... except Exception as e: raise FailedMessageException(msg) from e ... >>> send_message("Test") Traceback (most recent call last): File "", line 2, in send_message ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 1, in File "", line 3, in send_message __main__.FailedMessageException: Test (Also, the use of "from" here causes a slightly different wording, which I think is more appropriate. But that's minor.) You get the report of which message failed, plus you get the complete traceback from the original exception. Much MUCH more useful. ChrisA