Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101053
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: raise None |
| Date | 2015-12-31 12:19 +0000 |
| Message-ID | <mailman.107.1451564424.11925.python-list@python.org> (permalink) |
| References | <56847239$0$1590$c3e8da3$5496439d@news.astraweb.com> <mailman.96.1451522295.11925.python-list@python.org> <5684842a$0$1596$c3e8da3$5496439d@news.astraweb.com> <mailman.97.1451526250.11925.python-list@python.org> <5684aa1a$0$1602$c3e8da3$5496439d@news.astraweb.com> |
On 31 December 2015 at 04:07, Steven D'Aprano <steve@pearwood.info> wrote:
> On Thu, 31 Dec 2015 12:44 pm, Ben Finney wrote:
>
>> Steven D'Aprano <steve@pearwood.info> writes:
>>
>>> Traceback (most recent call last):
>>> File "spam", line 19, in this
>>> File "spam", line 29, in that
>>> File "spam", line 39, in other
>>> File "spam", line 5, in _validate
>>> ThingyError: ...
>>>
>>> and the reader has to understand the internal workings of _validate
>>> sufficiently to infer that this exception is not a bug in _validate
>>> but an expected failure mode of other when you pass a bad argument.
>>
>> This point seems to advocate for suppressing *any* code that
>> deliberately raises an exception. Is that your intent?
>
> No. The issue isn't that an exception is deliberately raised. The issue is
> that it is deliberately raised in a function separate from where the
> exception conceptually belongs. The exception is conceptually part of
> function "other", and was only refactored into a separate function
> _validate to avoid repeating the same validation code in multiple places.
> It is a mere implementation detail that the exception is actually raised
> inside _validate rather than other.
>
> As an implementation detail, exposing it to the user (in the form of a line
> in the stacktrace) doesn't help debugging. At best it is neutral (the user
> reads the error message and immediately realises that the problem lies with
> bad arguments passed to other, and _validate has nothing to do with it). At
> worst it actively misleads the user into thinking that there is a bug in
> _validate.
You're overthinking this. It's fine for the error to come from
_validate. Conceptually the real error is not in _validate or the
function that calls _validate but in whatever function further up the
stack trace created the wrong type of object to pass in. If the user
can see the stack trace and work back to the point where they passed
something in to your function then how does the extra level hurt?
If it really bothers you then you can use a comment that will show up
in the traceback output
_validate(a, b) # Verify arguments to myfunc(a, b)
but really I don't think it's a big deal. The traceback gives you
useful information about where to look for an error/bug but it's still
the programmer's job to interpret that, look at the code, and try to
understand what they have done to cause the problem.
--
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 11:09 +1100
Re: raise None Paul Rubin <no.email@nospam.invalid> - 2015-12-30 16:19 -0800
Validation in Python (was: raise None) Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 11:26 +1100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 11:38 +1100
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 12:26 +1100
Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 12:44 +1100
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 15:07 +1100
Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 12:19 +0000
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 02:35 +1100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 02:53 +1100
Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 16:46 +0000
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 04:50 +1100
Re: raise None "Martin A. Brown" <martin@linux-ip.net> - 2015-12-31 09:30 -0800
Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 07:18 +1100
Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2016-01-02 12:47 +0100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 09:48 +1100
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
Re: raise None Dan Sommers <dan@tombstonezero.net> - 2016-01-04 06:09 +0000
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 22:39 -0800
Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 10:27 +1100
Re: raise None Marko Rauhamaa <marko@pacujo.net> - 2016-01-01 02:29 +0200
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-04 03:55 -0800
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 23:36 +0000
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:39 +1100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:41 +1100
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 19:04 -0800
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:31 +1100
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 14:48 +1100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:56 +1100
Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 20:46 -0800
Re: raise None Christian Gollwitzer <auriocus@gmx.de> - 2016-01-04 08:28 +0100
Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 13:12 +1100
Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 15:03 +1100
Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 16:12 +1100
Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 16:45 +1100
Re: raise None Terry Reedy <tjreedy@udel.edu> - 2015-12-30 23:00 -0500
Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 15:58 +0000
Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-12-31 17:17 +0100
csiph-web