The Right Approach to Exceptions

H. S. Teoh hsteoh at
Sun Feb 19 01:15:58 PST 2012

On Sun, Feb 19, 2012 at 02:02:39AM -0600, Andrei Alexandrescu wrote:
> On 2/19/12 12:54 AM, H. S. Teoh wrote:
> >On Sun, Feb 19, 2012 at 12:43:58AM -0600, Andrei Alexandrescu wrote:
> >>I'm thinking an error is transient if retrying the operation with
> >>the same exact data may succeed. That's a definition that's simple,
> >>useful, and easy to operate with.
> >[...]
> >
> >But if that's the case, what's the use of an exception at all?
> Centralization.

I don't understand.  So instead of providing enough information to the
caller about the nature of the problem, you're essentially handing them
an anonymous note saying "A generic problem occurred, which _may_ go
away if you retry. I have no further information for you. Do you want to

But without further information, how *can* you even make that decision?
Without any way of determining what caused the error or even what it is,
how could you know whether it makes sense to retry it?

Or is that transient flag intended to mean that it *should* be retried
since it "might" succeed the next time round? How should the caller
decide whether or not to go ahead with the retry? Flip a coin? Always
retry?  Always fail? I can't imagine any sane application where code
would say "if this operation fails with a transient error, always fail"
where any arbitrary set of exceptions might potentially be transient?
What's a "transient error" anyway, from the application's POV anyway?
What's a "transient error" from a database app's POV? A 3D shooter? A
text editor? Is it even possible to have a consistent definition of
"transient" that is meaningful across all applications?

It seems to me that if an error is "transient", then the called function
might as well just always retry it in the first place, instead of
throwing an exception.  Such an exception is completely meaningless to
the caller without further information. Yet it seems that you are
suggesting that it's more meaningful than FileNotFoundException?


Obviously, some things aren't very obvious.

More information about the Digitalmars-d mailing list