The Right Approach to Exceptions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 21 06:20:22 PST 2012


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