exception types & objects

Jonathan M Davis jmdavisProg at gmx.com
Tue Oct 19 15:23:56 PDT 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


More information about the Digitalmars-d-learn mailing list