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


Groups > comp.lang.python > #57405

Re: Re-raising a RuntimeError - good practice?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <robotsondrugs@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'example:': 0.03; 'cpython': 0.05; 'debugging': 0.07; 'happen,': 0.09; 'method,': 0.09; 'try:': 0.09; 'def': 0.12; 'windows': 0.15; "%s'": 0.16; 'suggested,': 0.16; 'unexpected': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'helpful': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'specifically': 0.29; 'raise': 0.29; 'statement': 0.30; 'especially': 0.30; 'skip:( 20': 0.30; 'received:10.0.0': 0.31; 'exceptions': 0.31; 'loading': 0.31; 'steven': 0.31; 'up.': 0.33; 'running': 0.33; 'except': 0.35; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'raising': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'received:10.0': 0.36; 'received:10': 0.37; 'skip:o 20': 0.38; 'message-id:@gmail.com': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'catch': 0.60; 'range': 0.61; "you're": 0.61; 'places': 0.64; 'export': 0.74; 'freebsd': 0.84; 'subject:good': 0.84; 'victor': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Hi3Mwm4y8qQx8NnsDLe246UKkoAHoaYdB1Hi2fcPhW4=; b=KXMF57Xr2EpME1rkHEoymRlJmodRpdSVgXlN+M/FUrfeEwo/nFC5ZPsRYyFCRVaDs4 VvtpkUXs0qRi1FCRiuHcY0Qice4HImZLchTmkJ1ZZh+9q8U1Nre4dDEytceKSrMlqJsC HAdfIkq6ezqoJ2LUqQ9SLHXw0FWEpkOWLtcq9alVYexO5jTfwy2LxxS/peOYoZZQvNXf ZdbwIc5rk2HCvMkT2H2vDGF/6N4X8t66M8y23gtYqr9c2PRfgZFS9f6djD6QlfKc0Tch tfxJWeRN3kY6zqdGf4N2eIXPKTKJ0iav3LTQ8mTraWXOq1PPgy5TdotfTqOnXPVxzhHG j76Q==
X-Received by 10.50.16.45 with SMTP id c13mr260116igd.55.1382589788767; Wed, 23 Oct 2013 21:43:08 -0700 (PDT)
Date Wed, 23 Oct 2013 23:42:53 -0500
From Andrew Berg <robotsondrugs@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To "comp.lang.python" <python-list@python.org>
Subject Re: Re-raising a RuntimeError - good practice?
References <670cb7a6-f8e9-4e3d-95a0-95728f8dd815@googlegroups.com>
In-Reply-To <670cb7a6-f8e9-4e3d-95a0-95728f8dd815@googlegroups.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1439.1382589791.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1382589791 news.xs4all.nl 15918 [2001:888:2000:d::a6]:59822
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57405

Show key headers only | View raw


On 2013.10.23 22:23, Victor Hooi wrote:
> For example:
> 
>     def run_all(self):
>         self.logger.debug('Running loading job for %s' % self.friendly_name)
>         try:
>             self.export_to_csv()
>             self.gzip_csv_file()
>             self.upload_to_foo()
>             self.load_foo_to_bar()
>         except RuntimeError as e:
>             self.logger.error('Error running job %s' % self.friendly_name)
> ...
>     def export_to_csv(self):
>     ...
>         try:
>             with open(self.export_sql_file, 'r') as f:
>                 self.logger.debug('Attempting to read in SQL export statement from %s' % self.export_sql_file)
>                 self.export_sql_statement = f.read()
>                 self.logger.debug('Successfully read in SQL export statement')
>         except Exception as e:
>             self.logger.error('Error reading in %s - %s' % (self.export_sql_file, e), exc_info=True)
>             raise RuntimeError
You're not re-raising a RuntimeError. You're swallowing all exceptions and then raising a RuntimeError. Re-raise the original exception in
export_to_csv() and then handle it higher up. As Steven suggested, it is a good idea to handle exceptions in as few places as possible (and
as specifically as possible). Also, loggers have an exception method, which can be very helpful in debugging when unexpected things happen,
especially when you need to catch a wide range of exceptions.

-- 
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 10.0

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


Thread

Re-raising a RuntimeError - good practice? Victor Hooi <victorhooi@gmail.com> - 2013-10-23 20:23 -0700
  Re: Re-raising a RuntimeError - good practice? Steven D'Aprano <steve@pearwood.info> - 2013-10-24 04:34 +0000
  Re: Re-raising a RuntimeError - good practice? Andrew Berg <robotsondrugs@gmail.com> - 2013-10-23 23:42 -0500
    Re: Re-raising a RuntimeError - good practice? Victor Hooi <victorhooi@gmail.com> - 2013-10-24 18:09 -0700
      Re: Re-raising a RuntimeError - good practice? Andrew Berg <robotsondrugs@gmail.com> - 2013-10-24 20:17 -0500
        Re: Re-raising a RuntimeError - good practice? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-25 02:30 +0000
  Re: Re-raising a RuntimeError - good practice? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-25 00:44 -0700

csiph-web