Throwable class design
Jonathan M Davis
jmdavisProg at gmx.com
Wed Jan 30 09:22:53 PST 2013
On Wednesday, January 30, 2013 10:05:21 rumbu wrote:
> On Wednesday, 30 January 2013 at 08:44:52 UTC, Jonathan M Davis
>
> wrote:
> > Exceptions get rethrown all the time. Heck, that's what happens
> > with finally,
> > scope(failure), and scope(exit). It's what you do when you need
> > to react to
> > the fact that the exception was thrown (maybe even to the
> > specific exception)
> > but aren't going to handle it in that section of code.
> >
> > Now, mutating the exception before rethrowing is another thing.
> > I'd tend to
> > think that that was bad practice, but I suppose that it could
> > be useful upon
> > occasion. In general though, if I wanted to add information,
> > I'd wrap the
> > caught exception in a new exception and throw that.
> >
> > - Jonathan M Davis
>
> Exceptions are not rethrown, finally block is executed *after*
> throwing/catching the exception. I don't know the details of the
> scope(*) implementations, but I doubt that they are rethrowing
> any exceptions. Exceptions are propagated, not rethrown with a
> mutated message or field.
You seem to think that rethrowing has something to do with mutation. It
doesn't. Finally, scope(failure), and scope(exit) all must catch exceptions,
run the code within their blocks, and then rethrow the exception. No mutation
of the exception is involved. If they didn't rethrow the exception, then it
would stop there and never escape those blocks, and if it weren't caught by
them, then those blocks wouldn't be executed at all.
Also I could easily do something like
try
throw new Exception("hello");
catch(Exception e)
{
writefln("It says: %s", e.msg);
throw e;
}
I catch and use the exception and then rethrow it, but I don't mutate it. Yes,
you can catch an exception, mutate it, and then rethrow it. But the fact that
it's rethrown says nothing about whether it was mutated. It just means that
the exception continues to propagate up again after it was caught.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list