Jump to page: 1 2
Thread overview
exception types & objects
Oct 19, 2010
spir
Oct 19, 2010
Simen kjaeraas
Oct 19, 2010
spir
Oct 20, 2010
Lutger
Oct 19, 2010
Kagamin
Oct 19, 2010
Kagamin
Oct 19, 2010
Denis Koroskin
Oct 19, 2010
div0
Oct 19, 2010
Stewart Gordon
Oct 19, 2010
Jonathan M Davis
Oct 21, 2010
Stewart Gordon
Oct 31, 2010
Don
October 19, 2010
Hello,

The reference states:

ThrowStatement:
	throw Expression ;
Expression is evaluated and must be an Object reference. The Object reference is thrown as an exception.
	throw new Exception("message");

The example uses an Excpetion type, but the somewhat comment contradicts it. Can one really use any kind of object as exception? How does D then handle it? I was expecting (from exp with other language) there to be a root Error or Exception type implementating base exception mechanics and/or an imposed interface to comply with (esp a kind of string method returning what text to write out when the exception is not caught).
How does this work, and how to use it concretely?

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

October 19, 2010
spir <denis.spir@gmail.com> wrote:

> Hello,
>
> The reference states:
>
> ThrowStatement:
> 	throw Expression ;
> Expression is evaluated and must be an Object reference. The Object reference is thrown as an exception.
> 	throw new Exception("message");
>
> The example uses an Excpetion type, but the somewhat comment contradicts it. Can one really use any kind of object as exception?

Absolutely. It is however heavily discouraged.


> How does D then handle it?

The way it does any other exception. You may catch it using
catch(Object o).


> I was expecting (from exp with other language) there to be a root Error or Exception type implementating base exception mechanics and/or an imposed interface to comply with (esp a kind of string method returning what text to write out when the exception is not caught).
> How does this work, and how to use it concretely?

There is a base class Exception and Error, as well as the base interface
Throwable, which the aforementioned implement. Using this is more of a
convention than absolute necessity, but it is used all over Phobos and
druntime, and it is heavily discouraged not to use those as bases.

On a sidenote, one should usually only catch Exceptions, not Errors, as
the latter is supposed to be used for non-recoverable errors.

-- 
Simen
October 19, 2010
On Tue, 19 Oct 2010 14:52:20 +0200
"Simen kjaeraas" <simen.kjaras@gmail.com> wrote:

> > I was expecting (from exp with other language) there to be a root Error
> > or Exception type implementating base exception mechanics and/or an
> > imposed interface to comply with (esp a kind of string method returning
> > what text to write out when the exception is not caught).
> > How does this work, and how to use it concretely?
> 
> There is a base class Exception and Error, as well as the base interface Throwable, which the aforementioned implement. Using this is more of a convention than absolute necessity, but it is used all over Phobos and druntime, and it is heavily discouraged not to use those as bases.

Good. Is this documented somewhere (have searched language and library references before asking: found some documents on peculiar aspects, but none describing the mechanism and how to use).

> On a sidenote, one should usually only catch Exceptions, not Errors, as the latter is supposed to be used for non-recoverable errors.

Very well, this matches my point of view.

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

October 19, 2010
spir Wrote:

> I was expecting (from exp with other language) there to be a root Error or Exception type implementating base exception mechanics and/or an imposed interface to comply with (esp a kind of string method returning what text to write out when the exception is not caught).

Exception mechanics consists of taking an object pointer, putting it into exception record, unwinding stack, catch takes the pointer, tests its runtime type against what you specified in catch statement and gives the pointer to user code. In fact, toString method is inherited from Object.
October 19, 2010
spir Wrote:

> The example uses an Excpetion type, but the somewhat comment contradicts it. Can one really use any kind of object as exception?

ps it's fun to throw strings in javascript, try it.
:3
October 19, 2010
On Tue, 19 Oct 2010 22:54:33 +0400, Kagamin <spam@here.lot> wrote:

> spir Wrote:
>
>> The example uses an Excpetion type, but the somewhat comment contradicts it. Can one really use any kind of object as exception?
>
> ps it's fun to throw strings in javascript, try it.
> :3

I've been throwing const char* in C++:

try {
	throw "not implemented";
} catch (const char* message) {
	printf("Exception: %s\n", message);
}
October 19, 2010
On 19/10/2010 20:03, Denis Koroskin wrote:
> On Tue, 19 Oct 2010 22:54:33 +0400, Kagamin <spam@here.lot> wrote:
>
>> spir Wrote:
>>
>>> The example uses an Excpetion type, but the somewhat comment
>>> contradicts it. Can one really use any kind of object as exception?
>>
>> ps it's fun to throw strings in javascript, try it.
>> :3
>
> I've been throwing const char* in C++:
>
> try {
> throw "not implemented";
> } catch (const char* message) {
> printf("Exception: %s\n", message);
> }

You evil man.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
October 19, 2010
On 19/10/2010 13:13, spir wrote:
<snip>
> The example uses an Excpetion type, but the somewhat comment
> contradicts it. Can one really use any kind of object as exception?

Yes.

> How does D then handle it?

The mechanism is class-agnostic.  The only thing it relies on is that it's an object of some class type.

> I was expecting (from exp with other
> language) there to be a root Error or Exception type implementating
> base exception mechanics and/or an imposed interface to comply with

Imposed interface to do what with the exception?

OK, so I can think of one example it would be nice to have: stack traces.  This could be implemented in Exception, I suppose.  But still, restricting throw to this class would be a puristic rather than practical step forward.

> (esp a kind of string method returning what text to write out when
> the exception is not caught).
<snip>

Object.toString()

Stewart.
October 19, 2010
On Tuesday, October 19, 2010 12:36:23 Stewart Gordon wrote:
> On 19/10/2010 13:13, spir wrote:
> <snip>
> 
> > The example uses an Excpetion type, but the somewhat comment contradicts it. Can one really use any kind of object as exception?
> 
> Yes.
> 
> > How does D then handle it?
> 
> The mechanism is class-agnostic.  The only thing it relies on is that it's an object of some class type.
> 
> > I was expecting (from exp with other
> > language) there to be a root Error or Exception type implementating
> > base exception mechanics and/or an imposed interface to comply with
> 
> Imposed interface to do what with the exception?
> 
> OK, so I can think of one example it would be nice to have: stack traces.  This could be implemented in Exception, I suppose.  But still, restricting throw to this class would be a puristic rather than practical step forward.

Perhaps, but there is no real benefit in throwing anything other than an Exception (or Error upon occasion) and if we allow you to throw anything than catch(Exception e) won't catch them all. It also would pretty much violate nothrow since nothrow guarantees that no Exceptions are thrown from that function, and if you can throw any old object anyway...

I'd never heard of being able to throw anything which wasn't Throwable in D before reading this thread, and I'm appalled that it's possible. I'm very tempted to create  an enhancement request requesting that it be made _im_possible. I'm aware of no value in it. It's poor practice, can create confusion, and opens a whole in the language as far as nothrow goes

- Jonathan M Davis
October 20, 2010
spir wrote:

> On Tue, 19 Oct 2010 14:52:20 +0200
> "Simen kjaeraas" <simen.kjaras@gmail.com> wrote:
> 
>> > I was expecting (from exp with other language) there to be a root Error or Exception type implementating base exception mechanics and/or an imposed interface to comply with (esp a kind of string method returning what text to write out when the exception is not caught). How does this work, and how to use it concretely?
>> 
>> There is a base class Exception and Error, as well as the base interface Throwable, which the aforementioned implement. Using this is more of a convention than absolute necessity, but it is used all over Phobos and druntime, and it is heavily discouraged not to use those as bases.
> 
> Good. Is this documented somewhere (have searched language and library references before asking: found some documents on peculiar aspects, but none describing the mechanism and how to use).

Unfortunately the relevant docs here seem to be a bit out of date.

look up nothrow: http://www.digitalmars.com/d/2.0/function.html

this is out of date wrt the throwable hierarchy: http://www.digitalmars.com/d/2.0/errors.html

contains related info, recommended: http://www.digitalmars.com/d/2.0/exception-safe.html

If you want more details you could look up the header files of druntime distributed with the dmd package, specifically:

src/druntime/import/object.di: contains the Throwable, Error and Exception interfaces

src/druntime/import/core/exception.di: contains a few derived Exceptions
« First   ‹ Prev
1 2