Checking for an exception

Cameron Simpson cs at zip.com.au
Sun Jun 25 00:07:38 EDT 2017


On 25Jun2017 13:47, Ben Finney <ben+python at benfinney.id.au> wrote:
>Steve D'Aprano <steve+python at pearwood.info> writes:
>> […] the result of passing a non-exception to raise is to raise an
>> exception, so I cannot trivially distinguish between "caller passes an
>> exception" and "caller passes a non-exception" (result is still an
>> exception).
>
>Yes, hence my characterising this problem as the caller's problem.
>
>I'd say: document the expectation that the value will be an exception,
>use it based on that specification, and let the caller deal with the
>consequences of violating that expectation.

I'm a "fail early" kind of guy, and to me Steve's approach is in the same 
spirit as raising ValueError when a function is handed invalid arguments.

Particularly if the mistake is easy to make, having one's attention brought to 
it immediately (at "declaration" time, since Steve's example is a decorator), 
seems very desirable.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list