The Right Approach to Exceptions

Juan Manuel Cabo juanmanuel.cabo at
Tue Feb 21 07:46:58 PST 2012

> I think he meant to say things have been like that for a while and there's no blood in the streets.

That's exactly what I meant.

And even if one makes those fields private, anyone can take a pointer
to the class or void[] or whatever and do a mess. (Java isn't exepmpt,
you can do a mess with reflection there).
So there is a minimum of trust that we put on APIs and code that
we call downstream. The same trust that one puts, to begin with,
when one expects that an exception will be thrown when an error happens.

Ruby and PHP are based on a lot of trust for instance!
Having the advantages of staticlly typed language doesn't mean
that one must turn the advantages into disadvantages and start
hammering screws because we love hammers.


On 02/21/2012 11:20 AM, Andrei Alexandrescu wrote:
> On 2/21/12 6:36 AM, Jacob Carlborg wrote:
>> On 2012-02-20 23:44, Juan Manuel Cabo wrote:
>>>> I still don't like the idea of using Variant[string], though.
>>>> (1) It doesn't allow compile-time type checking. This is a big minus, in
>>>> my book.
>>> When you need compile-time type checking, define a variable in your
>>> class.
>>> Just make sure that you are creating that new exception class for a
>>> good reason.
>>> When a user needs to add a variable to the exception, he can add it
>>> without putting your exception class chained in a new type of exception,
>>> that will hide your class from being selected by upstream catch blocks
>>> in the call tree.
>>>> (2) It's overly flexible. Anyone along the call stack can insert
>>>> (hopefully NOT delete!!) additional data into the Exception object, as
>>>> the stack is unwound.
>>> As is currently the case.
>>> Did you know that anyone can overwrite any field of the exception and
>>> rethrow it? Such as msg field and so on?
>> No one says the fields need to be public instance variables. You could
>> take the arguments in the constructor and only have getters.
> I think he meant to say things have been like that for a while and there's no blood in the streets.
> Andrei

More information about the Digitalmars-d mailing list