Why exceptions for error handling is so important
Tobias M via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jan 15 13:48:24 PST 2015
On Thursday, 15 January 2015 at 21:28:59 UTC, H. S. Teoh via
Digitalmars-d wrote:
> Unless, of course, the *purpose* of the program is specifically
> to deal
> with problem situations -- in which case, you wouldn't be using
> exceptions to indicate those situations, you'd treat them as
> "normal"
> input instead. You wouldn't be using try/throw/catch, but normal
> flow-control constructs.
But for almost every environmental error, there's a use case
where it is normal or at least expected. That means, you have to
have two versions of every function, one that throws and one that
uses "normal" flow control.
Example:
Creating a file: Throws if already exists.
Creating a unique filename:
Create file.1.exp
Create file.2.exp
Create file.3.exp
... until it succeeds.
Here failure is expected and normal, still trial/error is the
only option because a querying the file first is not safe.
How can I (as the implementer of the "Create file" function)
decide if a failure is actually expected or not?
I can't, because I cannot foresee every use case of the function.
*Especially* with environmental errors that caller has to decide
what's an error and what not.
You cannot just require certain environmental preconditions,
because they can change unexpectedly.
More information about the Digitalmars-d
mailing list