March 19, 2009
The language reference says:

Throw Statement
Throw an exception.

ThrowStatement:
	throw Expression ;

Expression is evaluated and must be an Object reference. The Object reference is thrown as an exception.

But the following compiles and runs and catches the RottenEgg.


import std.stdio;

class RottenEgg
{
   string message;
   this(string s) { message = s; }
}

void main()
{
   try
   {
      throw new RottenEgg("something smelly");
   }
   catch (Exception e)
   {
      writefln("Caught exception ", e.toString());
   }
   catch (RottenEgg err)
   {
      writefln("Caught RottenEgg exception ", err.message);
   }
}

What is the point of class Throwable if you can throw any old class object?

March 19, 2009

Steve Teale wrote:
> ...
> 
> What is the point of class Throwable if you can throw any old class object?

What class Throwable?

  -- Daniel
March 19, 2009
Daniel Keep Wrote:

> 
> 
> Steve Teale wrote:
> > ...
> > 
> > What is the point of class Throwable if you can throw any old class object?
> 
> What class Throwable?

dmd\src\druntime\src\compiler\dmd\object_.d

> 
>   -- Daniel

March 19, 2009

Steve Teale wrote:
> Daniel Keep Wrote:
> 
>>
>> Steve Teale wrote:
>>> ...
>>>
>>> What is the point of class Throwable if you can throw any old class object?
>> What class Throwable?
> 
> dmd\src\druntime\src\compiler\dmd\object_.d
> 
>>   -- Daniel

Ah.

Well, if I had to guess, I'd say it was because Throwable objects are more useful than random objects of other types.  Object doesn't give you msg, file, line, info or next.

  -- Daniel
March 19, 2009
Daniel Keep Wrote:

> 
> 
> Steve Teale wrote:
> > Daniel Keep Wrote:
> > 
> >>
> >> Steve Teale wrote:
> >>> ...
> >>>
> >>> What is the point of class Throwable if you can throw any old class object?
> >> What class Throwable?
> > 
> > dmd\src\druntime\src\compiler\dmd\object_.d
> > 
> >>   -- Daniel
> 
> Ah.
> 
> Well, if I had to guess, I'd say it was because Throwable objects are more useful than random objects of other types.  Object doesn't give you msg, file, line, info or next.
> 
>   -- Daniel

But doesn't it rather imply that if you want to be able to throw a class, then it should be derived from Throwable?
March 19, 2009
On Thu, 19 Mar 2009 06:09:40 -0400, Steve Teale wrote:

> Daniel Keep Wrote:
> 
> 
>> 
>> Steve Teale wrote:
>> > Daniel Keep Wrote:
>> > 
>> > 
>> >> Steve Teale wrote:
>> >>> ...
>> >>>
>> >>> What is the point of class Throwable if you can throw any old class object?
>> >> What class Throwable?
>> > 
>> > dmd\src\druntime\src\compiler\dmd\object_.d
>> > 
>> >>   -- Daniel
>> 
>> Ah.
>> 
>> Well, if I had to guess, I'd say it was because Throwable objects are more useful than random objects of other types.  Object doesn't give you msg, file, line, info or next.
>> 
>>   -- Daniel
> 
> But doesn't it rather imply that if you want to be able to throw a class, then it should be derived from Throwable?

No, it just means that a Throwable object will provide a specific set of information.
March 19, 2009
Steve Teale wrote:
> 
> What is the point of class Throwable if you can throw any old class object?

I think a case could be made for allowing only children of Throwable to be thrown.  It's possible to throw integers and strings in C++, for example, which is totally ridiculous.  However, at the moment Throwable simply serves as the common base class for Exception and Error, and holds file and line info as well as the stack trace hooks.
March 19, 2009
Steve Teale wrote:
> Daniel Keep Wrote:
> 
>>
>> Steve Teale wrote:
>>> Daniel Keep Wrote:
>>>
>>>> Steve Teale wrote:
>>>>> ...
>>>>>
>>>>> What is the point of class Throwable if you can throw any old class object?
>>>> What class Throwable?
>>> dmd\src\druntime\src\compiler\dmd\object_.d
>>>
>>>>   -- Daniel
>> Ah.
>>
>> Well, if I had to guess, I'd say it was because Throwable objects are
>> more useful than random objects of other types.  Object doesn't give you
>> msg, file, line, info or next.
>>
>>   -- Daniel
> 
> But doesn't it rather imply that if you want to be able to throw a class, then it should be derived from Throwable?

Yes, a check should be added to that effect. Later on we need to actually exploit that only certain types can be thrown in implementing exception chaining.

What is exception chaining? Consider:

void weird()
{
    scope(failure) throw new Exception1("hum");
    throw new Exception2("ho");
}

The C++ model terminates the app when an exception is thrown while the stack is unwound. Java (I was told without having checked) makes the latest exception thrown the "main" exception, and you get to also access the other exceptions by using primitives in class Exception. Is that true?

Anyhow, we should implement a model in D that allows such multiple throws. Then a few primitives defined in class Exception - e.g. next() - should make the entire chain accessible. At some point Walter started implementing that but other priorities took over. A great side project for anyone if you ask me :o).


Andrei
March 19, 2009
Andrei Alexandrescu wrote:
> Steve Teale wrote:
>> Daniel Keep Wrote:
>>
>>>
>>> Steve Teale wrote:
>>>> Daniel Keep Wrote:
>>>>
>>>>> Steve Teale wrote:
>>>>>> ...
>>>>>>
>>>>>> What is the point of class Throwable if you can throw any old class object?
>>>>> What class Throwable?
>>>> dmd\src\druntime\src\compiler\dmd\object_.d
>>>>
>>>>>   -- Daniel
>>> Ah.
>>>
>>> Well, if I had to guess, I'd say it was because Throwable objects are
>>> more useful than random objects of other types.  Object doesn't give you
>>> msg, file, line, info or next.
>>>
>>>   -- Daniel
>>
>> But doesn't it rather imply that if you want to be able to throw a class, then it should be derived from Throwable?
> 
> Yes, a check should be added to that effect. Later on we need to actually exploit that only certain types can be thrown in implementing exception chaining.
> 
> What is exception chaining? Consider:
> 
> void weird()
> {
>     scope(failure) throw new Exception1("hum");
>     throw new Exception2("ho");
> }
> 
> The C++ model terminates the app when an exception is thrown while the stack is unwound. Java (I was told without having checked) makes the latest exception thrown the "main" exception, and you get to also access the other exceptions by using primitives in class Exception. Is that true?

You can't throw multiple exceptions in Java. What you can do is:

try {
  ...
} catch(SomeException e) {
  throw new AnotherException(e);
}

and then AnotherException is thrown with "e" being it's inner exception, and you can check that.

> 
> Anyhow, we should implement a model in D that allows such multiple throws.

What for?
March 19, 2009
Ary Borenszweig wrote:
> Andrei Alexandrescu wrote:
>> Steve Teale wrote:
>>> Daniel Keep Wrote:
>>>
>>>>
>>>> Steve Teale wrote:
>>>>> Daniel Keep Wrote:
>>>>>
>>>>>> Steve Teale wrote:
>>>>>>> ...
>>>>>>>
>>>>>>> What is the point of class Throwable if you can throw any old class object?
>>>>>> What class Throwable?
>>>>> dmd\src\druntime\src\compiler\dmd\object_.d
>>>>>
>>>>>>   -- Daniel
>>>> Ah.
>>>>
>>>> Well, if I had to guess, I'd say it was because Throwable objects are
>>>> more useful than random objects of other types.  Object doesn't give you
>>>> msg, file, line, info or next.
>>>>
>>>>   -- Daniel
>>>
>>> But doesn't it rather imply that if you want to be able to throw a class, then it should be derived from Throwable?
>>
>> Yes, a check should be added to that effect. Later on we need to actually exploit that only certain types can be thrown in implementing exception chaining.
>>
>> What is exception chaining? Consider:
>>
>> void weird()
>> {
>>     scope(failure) throw new Exception1("hum");
>>     throw new Exception2("ho");
>> }
>>
>> The C++ model terminates the app when an exception is thrown while the stack is unwound. Java (I was told without having checked) makes the latest exception thrown the "main" exception, and you get to also access the other exceptions by using primitives in class Exception. Is that true?
> 
> You can't throw multiple exceptions in Java. What you can do is:
> 
> try {
>   ...
> } catch(SomeException e) {
>   throw new AnotherException(e);
> }
> 
> and then AnotherException is thrown with "e" being it's inner exception, and you can check that.
> 
>>
>> Anyhow, we should implement a model in D that allows such multiple throws.
> 
> What for?

For allowing destructors to throw.

Andrei
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home