K
Krice
3. They don't understand them.
Would be great if someone could explain in simple way
what exception is. Why, how and when it should be used.
3. They don't understand them.
Effective C++ 3rd edition, very few items are about exceptions and there
is all you need to know.
"dragan" ha scritto nel messaggionews:[email protected]...
12. they not have "the written form"
"if(a/b > c) u=z;"
if "a/b" throw one exception
in the plain text above;
there is not a single char in
"if(a/b > c) u=z;"
that show this chanches
13. the "exception header" not "know" the point of code
where is the problem. Instead returning the error-result of the
function the program know where is the error
James said:"dragan" ha scritto nel messaggio
[...]
12. they not have "the written form""if(a/b > c) u=z;"if "a/b" throw one exception
in the plain text above;
there is not a single char in
"if(a/b > c) u=z;"
that show this chanches
That's probably because there is no such chance.
But seriously, this is the one real problem with exceptions. And
the possibility of exceptions does require some additional
thought. But the problem has been addressed, and we do know
what has to be done.
That's the whole point of exceptions. They're for the sort of
problems where it doesn't matter where the problem occurred; the
handling is the same. If you run out of memory processing a
request, for example, it doesn't matter where you were in the
processing when you ran out; you just abort the request (but not
the process) with an error message, and continue.
James said:[...]"dragan" ha scritto nel
messaggionews:[email protected]...
But seriously, this is the one real problem with exceptions.
And the possibility of exceptions does require some
additional thought. But the problem has been addressed, and
we do know what has to be done.
Yeah, exceptions are a bitch that way. They force us to think
and write correct programs.
Yeah, exceptions are a bitch that way. They force us to thinkJames said:"dragan" ha scritto nel
messaggio [...]
But seriously, this is the one real problem with exceptions.
And the possibility of exceptions does require some
additional thought. But the problem has been addressed, and
we do know what has to be done.
and write correct programs.
They introduce hidden flow paths which have to be considered
when evaluating program correctness. The fact that they don't
appear in the source code does increase the chance that they are
overlooked, and the fact that a lot of functions aren't
guaranteed to be no throw, although in fact they are, and if
return codes were used, they would still return void, increases
the actual workload, since without the nothrow guarantee, you
have to consider that they might throw.
How much extra effort and risk this entails has to be weighed
against the extra effort and risk of not using exceptions. Most
of the time, exceptions win.
When not using exceptions, you more or less write the hidden error-
returning path explicitly in your code. In my experience, this often
gives a signicifand increase in source-code size and also obscures the
algorithm. This is a source of bugs, and the effort required to
explicite the path is normally much larger than the effort used to
write exception safe code.
They introduce hidden flow paths which have to be consideredJames Kanze wrote:
"dragan" ha scritto nel
messaggio[...]
But seriously, this is the one real problem with
exceptions. And the possibility of exceptions does
require some additional thought. But the problem has
been addressed, and we do know what has to be done.
Yeah, exceptions are a bitch that way. They force us to
think and write correct programs.
when evaluating program correctness. The fact that they
don't appear in the source code does increase the chance
that they are overlooked, and the fact that a lot of
functions aren't guaranteed to be no throw, although in fact
they are, and if return codes were used, they would still
return void, increases the actual workload, since without
the nothrow guarantee, you have to consider that they might
throw.
Yes, but 99% of the time what is required is that your classes
have the basic exception guarantee - that you use RAII. The
last one percent you have to do some manual roll-back, but
that code would have to exist also in code not using
exceptions.
When not using exceptions, you more or less write the hidden
error-returning path explicitly in your code.
In my experience, this often gives a signicifand increase in
source-code size and also obscures the algorithm.
This is a source of bugs, and the effort required to explicite
the path is normally much larger than the effort used to write
exception safe code.
James said:Even within a single application, different types of errors will
require different types of handling.
For that matter, the same
type of error may require different handling in different
contexts: a write error in a log file should probably be
ignored; a write error in the program output (e.g. when writing
the object file in a compiler) should probably result in the
program terminating with an error status (after clean-up---I
can't think of any case where a write error should result in an
abort).
Which is what I said. Most of the time, aborting is the
appropriate reaction,
and it is the obvious choice for a default
action,
but I'm well aware that there are exceptions: game
programs, for example, and possibly light weight GUI client
code.
I don't think anyone has reasonably argued that exceptions are
an "error managment strategy". First, as you say, they are a
just a mechanism---a tool to implement a strategy, and not a
strategy. And second, they only address one aspect of error
management: reporting.
Error management involves three aspects:
detection, reporting and handling.
Exceptions don't help in
detection, nor in handling. They just simplify reporting when
the error must be handled far up the call stack.
(Which means
that they are of no use when the error must be handled
immediately.)
I didn't elevate anything. I'm just describing what exception
specifications do.
It is. Since the standard says it is. A function with an
exception specification "throw (std::bad_alloc)" may not exit by
any exception other than std::bad_alloc or an exception which
derives from std::bad_alloc. And the standard is authoritive.
Whether such a "contract" is practically useful is another
issue.
In practice, the only contract expressible by exception
specifications which I've found useful is "throw()". In
otherwords, that the function will not exit by an exception,
period. Without it, you can't really write exception safe code.
I'm having difficulty parsing what you're trying to say. But
programming by contract certainly hasn't failed, and is still
considered a good thing in most circles. As for exception
specifications, they're just another part of the contract. So
one can't say that they've failed.
What one can say is that the
contract they express is not all that useful
(with the exception
of an empty exception specification---a no-throw guarantee).
I didn't write the definition of exception specifications. It's
in the standard.
I didn't write that, so I can't be sure, but a priori, an
"unexpected exception" is one that the programmer didn't expect;
that he wasn't prepared to handle.
James said:I think you misunderstood my statement. I explicitly said "Just
a linguistic nit". All I disagreed with was his use of one
particular word.
And that use, while not formally correct, is
quite idiomatic in English: you'll quite often hear "an
unprecedented amount of X" as an exagerated expression of "a lot
of X".
I don't think any amount of misconceptions would be
"unprecedented", given the number of sites on the Web by authors
who clearly don't understand what they're talking about. But
the site in question clearly falls into the category of sites
pontificating on subjects which the authors don't know very
well, if at all.
James said:"dragan" ha scritto nel
messaggio
[...]
12. they not have "the written form""if(a/b > c) u=z;"if "a/b" throw one exception
in the plain text above;
there is not a single char in
"if(a/b > c) u=z;"
that show this chanches
That's probably because there is no such chance.
But seriously, this is the one real problem with exceptions. And
the possibility of exceptions does require some additional
thought. But the problem has been addressed, and we do know
what has to be done.
13. the "exception header" not "know" the point of code
where is the problem. Instead returning the error-result of the
function the program know where is the error
That's the whole point of exceptions. They're for the sort of
problems where it doesn't matter where the problem occured; the
handling is the same. If you run out of memory processing a
request, for example, it doesn't matter where you were in the
processing when you ran out; you just abort the request (but not
the process) with an error message, and continue.
James said:James said:"dragan" ha scritto nel
messaggio
[...]
But seriously, this is the one real problem with exceptions.
And the possibility of exceptions does require some
additional thought. But the problem has been addressed, and
we do know what has to be done.Yeah, exceptions are a bitch that way. They force us to think
and write correct programs.
They introduce hidden flow paths which have to be considered
when evaluating program correctness. The fact that they don't
appear in the source code does increase the chance that they are
overlooked, and the fact that a lot of functions aren't
guaranteed to be no throw, although in fact they are, and if
return codes were used, they would still return void, increases
the actual workload, since without the nothrow guarantee, you
have to consider that they might throw.
How much extra effort and risk this entails has to be weighed
against the extra effort and risk of not using exceptions.
Most of the time, exceptions win.
peter said:James Kanze wrote:
"dragan" ha scritto nel
messaggio[...]
But seriously, this is the one real problem with exceptions.
And the possibility of exceptions does require some
additional thought. But the problem has been addressed, and
we do know what has to be done.
Yeah, exceptions are a bitch that way. They force us to think
and write correct programs.
They introduce hidden flow paths which have to be considered
when evaluating program correctness. The fact that they don't
appear in the source code does increase the chance that they are
overlooked, and the fact that a lot of functions aren't
guaranteed to be no throw, although in fact they are, and if
return codes were used, they would still return void, increases
the actual workload, since without the nothrow guarantee, you
have to consider that they might throw.
Yes, but 99% of the time what is required is that your classes have
the basic exception guarantee - that you use RAII. The last one
percent you have to do some manual roll-back, but that code would have
to exist also in code not using exceptions.
How much extra effort and risk this entails has to be weighed
against the extra effort and risk of not using exceptions. Most
of the time, exceptions win.
When not using exceptions, you more or less write the hidden error-
returning path explicitly in your code. In my experience, this often
gives a signicifand increase in source-code size
and also obscures the
algorithm.
This is a source of bugs,
and the effort required to
explicite the path is normally much larger than the effort used to
write exception safe code.
Unless you are Google!?- Skjul tekst i anførselstegn -
Can you give some numbers?
I don't see why. Hide the details with macros.
Yes, probably, without any formalism such as macros. I guess you could go a
step further and write a preprocessor do do some instrumentation for you
also.
I think that would be the same or maybe harder with exceptions because of
the interactions with other C++ stuff (not that I can name that other stuff
though). Ensuring cleanup and RAII and call order and the like is an
on-going thing no matter what your EH strategies are.
Not any exact ones, but I have been working in places where exceptions
were not used (due to some code being quite old and no one caring
about fixing). Code there typically looked something like:
int func(parm p,std::string &result)
{
std::string s;
int error;
result = func_1(parm,s);
if (error != OK)
{
return error;
}
error = func_2(s);
if (error != OK)
{
return error;
}
result = s;
return OK;
}
instead of the much simpler
std::string func(parm)
{
return func_2(func_1(parm));
}
int func(parm p,std::string &result)
{
std::string s;
int error;
result = func_1(parm,s);
if (error != OK)
{
return error;
}
error = func_2(s);
if (error != OK)
{
return error;
}
result = s;
return OK;
}
instead of the much simpler
std::string func(parm)
{
return func_2(func_1(parm));
}
But that was my point: "one and only" mechanism is the
"looking for the silver bullet" approach.
Reaction to what is the question. You must have something(s)
specific in mind.
Along with logging, backtracing, notification, perhaps
restarting from checkpoint, maybe rollback, maybe switching to
redundant system 3... Application-specific. Perhaps the only
place abort is appropriate is in language newsgroups!
They don't do that at all. Exceptions are an error propogation
mechanism in C++,
an probably best to use sparingly for places where local
"handling" is not possible (constructors, for one) or not
appropriate (independently developed libraries).
Exactly.
Reporting comes after exceptions have done their part.
There is also: error definition & categorization and propogation.
So I see at least 5 things. If I ponder a bit more here, I may
come up with a few more things.
I do know that I wouldn't call simply
detection/handling/reporting "error management" though, and
that's not what I meant when I used the terms. Documentation,
communication and enforcement of the prescribed or chosen
strategies and patterns are a few more (see, I came up with a
few more in 1 minute). "Error management" as simply
detection/handling/reporting? Nah. No way.
That's propogation, not reporting.
Catch = handle error
Yes, but having to write a try block, then a catch statement, is
more complicated than an if, if you want to process the error
immediately. You can use an exception, but it's about like
pealing a grape with a butcher's cleaver.
I don't really understand why the violation check's being done
at runtime rather than compile time (the way checked
exceptions in Java work) - the "what should happen if an
unexpected exception is thrown at runtime?" question is fair
enough, but why does it get to that point in the first place?
Is it difficult/impossible to do the check at compile time in
C++? Or is the issue that it would break a lot of old code
where the functions don't have exception specifications?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.