Re: Exception handling?
On Jun 24, 2:57 pm, Hector Santos <sant9...@nospam.gmail.com> wrote:
Of course not. For the most part, you just do
if (!myfunc(params))
return false;
(or equivalent thereof)
Could be, sure. For me, for example, logging functions:
FILE *fv = fopen(filename, "at");
if (fv) {
fprintf(fv,whatever);
fclose(fv);
}
Yes, this is a (well known? perhaps) issue when going exceptions. You
need to turn things on their heads somewhat. Supposing that you throw,
whatever happens, somewhere down the line you have to report this
error. The best way to do this is to have nice "original" error info.
In this case, that's relatively easy, because you have file name and
errno - all is there. So for logging, there's, again, nothing to do if
you use exceptions: error will propagate to somewhere up the stack,
and.. Surely you won't forget to log it. But! But... It's
__essential__ not to lose sight of the original error and to be
comprehensive with error info.
Now, supposing that you have several places where you might get into a
particular situation, and after seeing the error in your log, you
don't know where that place is (if you will, you know the line, but
it's important t know where did you come from to it). Then, you have
two possibilities:
1. have "debug" logging, turn it on and look there to see where things
start to fall apart. Combine with error log and there you are.
2. "enhance" original error while unwinding the stack (add "context"
if you will).
At any rate, I am pretty certain that exceptions won't really stop you
from getting the desired result. But one has too stop with error-
return thinking first (don't be mad at me, but I believe you can't let
that go). Instead, you have to think this way: any given line can
throw, bar those specifically crafted not to (and they are rare and
hence +/- easy to spot). Next, you start thinking in terms of
exception-safety guarantees for any code snippet/function. For
example, any resource allocation might need "protection".
but there are cases where in my more elaborate class based logging
class, where there is rotating of files, etc, there are some error
checking, including a general catch all for the valist.
Typical error-return code looks like the above. IOW, for the most part
(like, 90% of cases), it does not try to "handle" any failure modes at
the place of the call.
Not quite sure if I follow, and if I did, I would not be among the
90%.
I really have to press you here: so what code do you have after a
failed call? Provide some examples, and I am pretty confident that,
even if you used exceptions, you could get what you need pretty easily
(that is, even if you "handle" errors, it's trivial to do what you do
either way, exceptions or error-return). Here's my bet: most of the
time, you have error cleanup, for which you should use RAII in e.g. C+
+, or "using" in C#, or you have logging (see above about that). In
rare places, you have "corrective" action. But I bet you that these
are __rare__.
Goran.