Throwable class design

rumbu rumbu at rumbu.ro
Thu Jan 31 23:53:15 PST 2013


On Friday, 1 February 2013 at 01:07:32 UTC, H. S. Teoh wrote:
> On Fri, Feb 01, 2013 at 01:47:35AM +0100, Andrej Mitrovic wrote:
>> On 2/1/13, Sean Kelly <sean at invisibleduck.org> wrote:
>> > I think the reason is mostly historic--the original exception
>> > classes had public members.  There's no reason why they 
>> > couldn't be
>> > hidden behind read-only properties though, other than the 
>> > potential
>> > to break existing code.
>> 
>> I'd rather we not, it's useful being able to modify msg and 
>> line
>> sometimes, e.g.:
>> http://forum.dlang.org/thread/uudharkmihxjymsxcyxq@forum.dlang.org#post-mailman.855.1359491827.22503.digitalmars-d-learn:40puremagic.com
>> 
>> Another example, wrapping `format` to inject the local file 
>> and line:
>> 
>> string fmt(string file = __FILE__, size_t line = __LINE__,
>> Args...)(string fmtStr, Args args)
>> {
>>     try
>>     {
>>         return format(fmtStr, args);
>>     }
>>     catch (FormatException exc)
>>     {
>>         exc.file = file;
>>         exc.line = line;
>>         throw exc;
>>     }
>> }
>> 
>> Saves me from having to read a broken stack trace or file and 
>> line
>> within Phobos.
>
> +1. Like this idea, I'll have to start using it to avoid the 
> headache of
> trying to figure out where something blew up when the 
> stacktrace points
> to some deep obscure code inside Phobos.
>
>
> T

I found this approach wrong, FormatException already contains 
useful information that you overwrite. The correct approach from 
my point of view is "throw new 
WhateverExceptionEvenFormatException("something wrong in fmt", 
file, line, exc)"; If Throwable was designed with encapsulation 
in mind, the code above was not be possible. Overwriting exc with 
new information will tell that the problem is in "fmt" not in 
"format".



More information about the Digitalmars-d mailing list