www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What is throwable

reply Steve Teale <steve.teale britseyeview.com> writes:
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?
Mar 19 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Steve Teale wrote:
 ...
 
 What is the point of class Throwable if you can throw any old class object?
What class Throwable? -- Daniel
Mar 19 2009
parent reply Steve Teale <steve.teale britseyeview.com> writes:
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
Mar 19 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Mar 19 2009
parent reply Steve Teale <steve.teale britseyeview.com> writes:
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?
Mar 19 2009
next sibling parent Jesse Phillips <jessekphillips gmail.com> writes:
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.
Mar 19 2009
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Mar 19 2009
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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?
Mar 19 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Mar 19 2009
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 19 Mar 2009 19:39:52 +0300, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:

 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
Are you sure this is sound?
Mar 19 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Denis Koroskin wrote:
 On Thu, 19 Mar 2009 19:39:52 +0300, Andrei Alexandrescu 
 <SeeWebsiteForEmail erdani.org> wrote:
 
 Ary Borenszweig wrote:
 Andrei Alexandrescu wrote:
 Anyhow, we should implement a model in D that allows such multiple 
 throws.
What for?
For allowing destructors to throw. Andrei
Are you sure this is sound?
Yes. The notion that throwing destructors should terminate the application pronto is a prejudice we got from C++. Andrei
Mar 19 2009
parent reply Don <nospam nospam.com> writes:
Andrei Alexandrescu wrote:
 Denis Koroskin wrote:
 On Thu, 19 Mar 2009 19:39:52 +0300, Andrei Alexandrescu 
 <SeeWebsiteForEmail erdani.org> wrote:

 Ary Borenszweig wrote:
 Andrei Alexandrescu wrote:
 Anyhow, we should implement a model in D that allows such multiple 
 throws.
What for?
For allowing destructors to throw. Andrei
Are you sure this is sound?
Yes. The notion that throwing destructors should terminate the application pronto is a prejudice we got from C++. Andrei
And it causes lots of problems, in my experience. Since it usually doesn't terminate the app, just the thread with the defective destructor. Which is unnecessarily difficult to debug.
Mar 19 2009
parent reply Sean Kelly <sean invisibleduck.org> writes:
== Quote from Don (nospam nospam.com)'s article
 Andrei Alexandrescu wrote:
 Denis Koroskin wrote:
 On Thu, 19 Mar 2009 19:39:52 +0300, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:

 Ary Borenszweig wrote:
 Andrei Alexandrescu wrote:
 Anyhow, we should implement a model in D that allows such multiple
 throws.
What for?
For allowing destructors to throw. Andrei
Are you sure this is sound?
Yes. The notion that throwing destructors should terminate the application pronto is a prejudice we got from C++. Andrei
And it causes lots of problems, in my experience. Since it usually doesn't terminate the app, just the thread with the defective destructor. Which is unnecessarily difficult to debug.
I've considered letting the user supply a custom "unhandled exception" handler for threads. It just seemed messy given that calling join() on a thread now will rethrow the unhandled exception if there was one. I figured that the user would join threads he cared about, and if one he didn't care about terminated unexpectedly the perhaps that's not a problem.
Mar 19 2009
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Thu, 19 Mar 2009 20:44:51 +0000 (UTC), Sean Kelly wrote:

 == Quote from Don (nospam nospam.com)'s article
 Andrei Alexandrescu wrote:
 Denis Koroskin wrote:
 On Thu, 19 Mar 2009 19:39:52 +0300, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:

 Ary Borenszweig wrote:
 Andrei Alexandrescu wrote:
 Anyhow, we should implement a model in D that allows such multiple
 throws.
What for?
For allowing destructors to throw. Andrei
Are you sure this is sound?
Yes. The notion that throwing destructors should terminate the application pronto is a prejudice we got from C++. Andrei
And it causes lots of problems, in my experience. Since it usually doesn't terminate the app, just the thread with the defective destructor. Which is unnecessarily difficult to debug.
I've considered letting the user supply a custom "unhandled exception" handler for threads. It just seemed messy given that calling join() on a thread now will rethrow the unhandled exception if there was one. I figured that the user would join threads he cared about, and if one he didn't care about terminated unexpectedly the perhaps that's not a problem.
The problem is when you have a worker thread which never terminates and sends notifications to the main thread. Like game logic thread versus main GUI thread. If logic thread throws, your game suddenly stops working for no apparent reason.
Mar 19 2009
parent Sean Kelly <sean invisibleduck.org> writes:
== Quote from Sergey Gromov (snake.scaly gmail.com)'s article
 Thu, 19 Mar 2009 20:44:51 +0000 (UTC), Sean Kelly wrote:
 I've considered letting the user supply a custom "unhandled
 exception" handler for threads.  It just seemed messy given that
 calling join() on a thread now will rethrow the unhandled exception
 if there was one.  I figured that the user would join threads he cared
 about, and if one he didn't care about terminated unexpectedly the
 perhaps that's not a problem.
The problem is when you have a worker thread which never terminates and sends notifications to the main thread. Like game logic thread versus main GUI thread. If logic thread throws, your game suddenly stops working for no apparent reason.
So wrap your logic in a try/catch: void worker() { try { doStuff(); } catch( Throwable t ) { notifyProgramOfFailure( t ); } } auto t = new Thread( &worker ); I think a case could be made for simply terminating the app on an unhandled exception in a thread, but logic errors are the programmer's responsibility.
Mar 19 2009
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
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.
Mar 19 2009
parent Steve Teale <steve.teale britseyeview.com> writes:
Sean Kelly Wrote:

 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.
It's exactly what you would expect based on the languages that D has borrowed from, but if it does not mean throwable, what's the point? I still don't know what the significance of Exception and Error might be.
Mar 19 2009