Throwable, Exception, and Error

Denis Koroskin 2korden at gmail.com
Sun May 3 13:14:39 PDT 2009


On Sun, 03 May 2009 22:12:00 +0400, Steve Teale <steve.teale at britseyeview.com> wrote:

> Christopher Wright Wrote:
>
>> Steve Teale wrote:
>> > Christopher Wright Wrote:
>> >
>> >> Frank Benoit wrote:
>> >>> Walter Bright schrieb:
>> >>>> Steve Teale wrote:
>> >>>>> If I want a catch-all catch, which should I be using.
>> >>>>>
>> >>>>> Out of habit I use Exception, but if I do that then I will miss  
>> some
>> >>>>> things thrown from Phobos.
>> >>>>>
>> >>>>> Is it the intention that Throwable be used for this purpose?
>> >>>>>
>> >>>> Yes.
>> >>> Why is it possible to throw an Object?
>> >>> I think "throw" and "catch" should be restricted to Throwable and
>> >>> derived types.
>> >> Because Phobos did not contain Throwable until the advent of  
>> druntime.
>> >> This was sufficiently recent that Walter has not yet modified the
>> >> compiler accordingly, or, most likely, noticed or decided to do so.
>> >
>> > When that is done, and the object being thrown has been checked out,  
>> would it be a good idea for the compiler to automatically populate the  
>> file and line members of the Throwable?
>>
>> Hrm. You want to assign the file and line numbers where the exception is
>> thrown, but not if you are rethrowing the exception.
>>
>> The runtime has something like _d_throw; if the signature included
>> __FILE__ and __LINE__ for the throw expression, that should work.
>
> OK, I did not think it through, but it would save a lot of typing grief;
>
>  ... , __FILE__, __LINE__);
>
> every time you wanted to be careful.
>

PHP allows function definition like this:

void foo(Args args, string fileName = __FILE__, int line = __LINE__)
{
   // do stuff
}

You call it like this: "foo(args);" and correct file name and line number is passed, i.e. default arguments are evaluated at call site. Can we use the same trick for exceptions? Here is an example:

class Throwable
{
    this(string fileName = __FILE__, int line = __LINE__) { ... }
}

class Exception : Throwable
{
    this(string reason, string fileName = __FILE__, int line = __LINE__)
    {
        super(fileName, line);
        // ...
    }
}




More information about the Digitalmars-d mailing list