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


Groups > comp.lang.python > #57405

Re: Re-raising a RuntimeError - good practice?

Date 2013-10-23 23:42 -0500
From Andrew Berg <robotsondrugs@gmail.com>
Subject Re: Re-raising a RuntimeError - good practice?
References <670cb7a6-f8e9-4e3d-95a0-95728f8dd815@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1439.1382589791.18130.python-list@python.org> (permalink)

Show all headers | 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