The Right Approach to Exceptions

Jacob Carlborg doob at me.com
Tue Feb 21 04:36:28 PST 2012


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.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list