[Tutor] Why subclassing exceptions?

Mac Ryan quasipedia at gmail.com
Thu Sep 29 08:35:24 CEST 2011


On Wed, 28 Sep 2011 10:15:07 -0400
"Prasad, Ramit" <ramit.prasad at jpmorgan.com> wrote:

> >If you are using asserts for data validation, then your code is
> >broken. The caller can disable every single assert, and hence remove
> >your data validation, by simply passing a command line switch when
> >calling your program.  
> 
> To be fair, there are plenty of situations where someone has enough
> control of their execution environment to prevent that from
> happening. I agree on principle and for readability/maintainability;
> if I saw that I would be highly confused as to why asserts were
> everywhere.

Thank you Alan, Steve, Ramit for all your answers, very appreciated and
useful.

I would like to point out that I am not using ``assert`` for data
validation. I am using it to understand if my code works. The reason
why I prefer asserts over exceptions is just what you pointed out.
Keeping building on Steve's simile: when I use asserts in my code, I'm
still at the car factory. I'm not trying to communicate anything to a
potential driver: I'm just trying to prevent myself to say "the car is
ready" if there is a major flow in its design.

In other words: rather than creating a specific
"EngineerForgotToDesignEngine" warning light, I want the
engineer to realise at a much earlier stage that she does need
to put an engine on the car before the car is ready for prime
time.

Maybe the initial example that generated this thread was misleading, as
a function is a typical example of a code that never knows what
parameters will be passed in.

All that said, I totally see your point and I swear that from now on I
will give a deeper thought to what to write after the word "raise"! :)

Thank you very much once more,
/mac


More information about the Tutor mailing list