digitalmars.D - What is throwable
- Steve Teale <steve.teale britseyeview.com> Mar 19 2009
- Daniel Keep <daniel.keep.lists gmail.com> Mar 19 2009
- Steve Teale <steve.teale britseyeview.com> Mar 19 2009
- Daniel Keep <daniel.keep.lists gmail.com> Mar 19 2009
- Steve Teale <steve.teale britseyeview.com> Mar 19 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Mar 19 2009
- Ary Borenszweig <ary esperanto.org.ar> Mar 19 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Mar 19 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Mar 19 2009
- Don <nospam nospam.com> Mar 19 2009
- Sean Kelly <sean invisibleduck.org> Mar 19 2009
- Sergey Gromov <snake.scaly gmail.com> Mar 19 2009
- Sean Kelly <sean invisibleduck.org> Mar 19 2009
- Jesse Phillips <jessekphillips gmail.com> Mar 19 2009
- "Denis Koroskin" <2korden gmail.com> Mar 19 2009
- Sean Kelly <sean invisibleduck.org> Mar 19 2009
- Steve Teale <steve.teale britseyeview.com> Mar 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?
Mar 19 2009
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
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
Steve Teale wrote:Daniel Keep Wrote:Steve Teale wrote:... What is the point of class Throwable if you can throw any old class object?
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
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?
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
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?
-- Daniel
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
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?
-- Daniel
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
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?
-- Daniel
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
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.
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
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.
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
== Quote from Don (nospam nospam.com)'s articleAndrei 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.
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
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
Thu, 19 Mar 2009 20:44:51 +0000 (UTC), Sean Kelly wrote:== Quote from Don (nospam nospam.com)'s articleAndrei 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.
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
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
== Quote from Sergey Gromov (snake.scaly gmail.com)'s articleThu, 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.
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
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?
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
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?
-- Daniel
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?
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.
For allowing destructors to throw. Andrei
Are you sure this is sound?
Mar 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.
Mar 19 2009
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









Sean Kelly <sean invisibleduck.org> 