From: Martin B. on
Daniel T. wrote:
> Seungbeom Kim <musiphil(a)bawi.org> wrote:
>> Daniel T. wrote:
>>> (....)
>> You seem to have a very narrow understanding of what can/should be done
>> in exception handlers. You almost seem to argue (or you may actually be
>> arguing), "Writing exception handlers means the exceptions are expected,
>> but exceptions are only for 'unexpected' cases, and thus you should try
>> not to write exception handlers at all."
>
> That's not what I'm arguing... If I had to distill it in one sentence, I
> might say something like, "all thrown exceptions should cause the
> program to shutdown/reset, possibly saving/cleaning up issues along the
> way."
>

If I wanted to shutdown/reset on errors, why use exceptions? I can just
call some error-handler (that may save something) and then call exit(1).
I don't need exceptions for that.

You statements seem to imply that you think that exceptions in C++ are
something fundamentally different from the Exceptions people know from
Java (or C#, or Python, or ...). Performance, usage and
non/existing-undefined-behaviour issues aside, I view them as the same
thing. I use them for errors(widely defined) than *can* be handled by
the program without a need to shutdown the program but that do not
belong to the normal control flow.

br,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel T. on
"Martin B." <0xCDCDCDCD(a)gmx.at> wrote:
> Daniel T. wrote:
> > Seungbeom Kim <musiphil(a)bawi.org> wrote:
> > > Daniel T. wrote:
> >
> > If I had to distill it in one sentence, I might say something like,
> > "all thrown exceptions should cause the program to shutdown/reset,
> > possibly saving/cleaning up issues along the way."
>
> If I wanted to shutdown/reset on errors, why use exceptions? I can
> just call some error-handler (that may save something) and then call
> exit(1). I don't need exceptions for that.

If you are writing a program using some library, and it just up and
decided to call an internal error handler and exit, I expect you would
be rather upset with the library provider, but I could be wrong. I know
I would be rather upset.

> You statements seem to imply that you think that exceptions in C++ are
> something fundamentally different from the Exceptions people know from
> Java (or C#, or Python, or ...). Performance, usage and
> non/existing-undefined-behaviour issues aside, I view them as the same
> thing. I use them for errors(widely defined) than *can* be handled by
> the program without a need to shutdown the program but that do not
> belong to the normal control flow.

"If an exception is expected and caught so that it has no bad effects on
the behavior of the program, then how can it be an error?" -- Stroustrup

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
Daniel T. wrote:
> John G Harris <news0(a)nospam.demon.co.uk> wrote:
>> Daniel T. wrote:
>>
>>> That's not what I'm arguing... If I had to distill it in one
>>> sentence, I might say something like, "all thrown exceptions should
>>> cause the program to shutdown/reset, possibly saving/cleaning up
>>> issues along the way."
>> I still don't understand why you think that exceptions *must not* be
>> used for any other purpose.
>
> "Must not" is a lot stronger than "should," especially when you stress
> it... I wouldn't go that far. What I'm saying is the same thing that
> Stroustrup says in section 14.5 of "The C++ Programming Language."
>
> Exception handling is a less structured mechanism than local control
> structures such as if and for and is often less efficient when an
> exception is actually thrown. Therefore, exceptions should be used
> only where the more traditional control structures are inelegant or
> impossible to use.
>
> ...
>
> [Use exceptions as alternate returns] can easily be overused and
> lead to obscure code. Whenever reasonable, one should stick to the
> "exception handling is error handling" view. When this is done,
> code is clearly separated into two categories: ordinary code and
> error-handling code. This makes code more comprehensible.

"Cause the program to shutdown/reset" is a lot stronger than "error
handling", and you maintain the former while quoting the latter.

For example, if opening a file or making a network connection fails,
you should certainly handle the error but you may not necessarily cause
the program to shutdown/reset.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
Martin B. wrote:

> My emphasis here is that I think that exceptions are a good tool for
> canceling a logical operation the program intended to take and that a
> failing operation by no means should imply to terminate the whole
> program. Now what such an operation could be is surely highly
> individual, but my feeling is that for many people it doesn't involve
> aborting the whole program.
>
> Note how I am not arguing to use exceptions for "alternate returns" but
> there are *many* shades of errors that can happen in an application and
> I firmly believe that there is a class of errors that are appropriately
> raised by exceptions and then *handled* - and handling doesn't imply
> aborting the app for me.

One example that comes to my mind:
Execptions are clearly a better variant of setjmp()/longjmp(). In
database applications I wrote transactions often were spread over dozens
of functions calling each other. Whenever the database system returned
with a deadlock error, this call tree was aborted with longjmp and the
transaction restarted at the original place. Execptions would have made
this much easier.

<pseudocode language="C">
while (1)
{
if ((error = setjmp()) != 0)
{
cleanup_data();
if (error == DEADLOCK)
abort_transaction();
else
{
...
break;
]
}
initialize_data();
start_transaction();
process_data();
commit_transaction();
cleanup_data();
break;
}
return error;
</pseudocode>
--
Viele Grüße,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]