 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mentat79@gmail.com Guest
|
Posted: Fri Dec 23, 2005 3:14 am Post subject: why arent C++ exceptions declared in function declaration? |
|
|
Hello,
In Java, if a function body has a throw(SomeException) statement, then,
this must be explicitly declared in the function declaration via a
throws SomeException clause.
(yes yes as long as SomeExceptions is a descendant of Throwable or
something like that)
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
Thanks!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zara Guest
|
Posted: Fri Dec 23, 2005 1:00 pm Post subject: Re: why arent C++ exceptions declared in function declarati |
|
|
On 22 Dec 2005 22:14:19 -0500, "mentat79 (AT) gmail (DOT) com"
<mentat79 (AT) gmail (DOT) com> wrote:
| Quote: | Hello,
In Java, if a function body has a throw(SomeException) statement, then,
this must be explicitly declared in the function declaration via a
throws SomeException clause.
(yes yes as long as SomeExceptions is a descendant of Throwable or
something like that)
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
|
First: Java is a really bad example, for two reasons: a) it is OT here
and b) it has two categories of exceptions: rutime errors and
exceptions, and you must declare only the latter.
In C++, if you declare an exception list, you must stick to it. If you
ever throw a different exception, you will have an unexpected
exception situation. Really Bad Thing. And you are not responsible for
the behaviour of any function you call.
Compilers must create code for testing the execptions on every
exception listed function, even when exception list is throw(), so
thta code grows. Not listing exevptions has the advantage of letting
any exception go fast and easily to its destination (catch or exit,
depending on the structure of the program), putting no overhead on the
functions that do not need exception processing.
If exception lists were compulsory, any programmer should be forced to
keep track of the exceptions thrown by functions called, thus
enforcing recompilation with every change from the libraries used.
BTW, that isone of the reasons why Java does not force runtime error
lists.
IMHO, Java is neither consistent nor safe in this respect, and C++ is,
at least, safe and clearly easy to use.
Best regards,
Zara
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Luke Meyers Guest
|
Posted: Fri Dec 23, 2005 1:06 pm Post subject: Re: why arent C++ exceptions declared in function declaratio |
|
|
It is not purely stylistic. The semantics of a C++ exception
specification ("throws declaration" [sic -- the 's' on the end is a
Java-ism]) are quite different from what you would expect based on the
Java idiom. Whereas a Java throws clause declares "these exceptions
may be thrown and must be checked (enforced by the compiler); no other
exceptions (other than RuntimeExceptions) will be thrown," a C++
exception specification declares "I promise that if I do throw an
exception, it will be one of these types -- or, if one occurs that's
not in this list, it will be thrown as a std::unexpected." The
compiler does not have to do any checking for you like in Java, so
putting in an exception specification doesn't force people to catch all
your declared exceptions.
They're not generally used (by informed programmers) because they
easily lead to confusion, and don't provide much in the way of useful
semantic guarantees. All they really do is provide documentation, and
that's what comments are for.
See Stroustrup 14.6 for more on these, and also see the relevant
section in Sutter's style book (101 C++ Coding Standards) or whatever
it's called.
L
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
P.J. Plauger Guest
|
Posted: Fri Dec 23, 2005 4:19 pm Post subject: Re: why arent C++ exceptions declared in function declarati |
|
|
<mentat79 (AT) gmail (DOT) com> wrote
| Quote: | In Java, if a function body has a throw(SomeException) statement, then,
this must be explicitly declared in the function declaration via a
throws SomeException clause.
(yes yes as long as SomeExceptions is a descendant of Throwable or
something like that)
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
|
C++ exception specifications are a paper tiger -- they were
standardized before they were implemented. Unsurprisingly,
they have proved not to be as useful as originally hoped.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Dec 23, 2005 4:22 pm Post subject: Re: why arent C++ exceptions declared in function declarati |
|
|
In article <1135274202.983557.310930 (AT) g47g2000cwa (DOT) googlegroups.com>,
"mentat79 (AT) gmail (DOT) com" <mentat79 (AT) gmail (DOT) com> writes
| Quote: | Hello,
In Java, if a function body has a throw(SomeException) statement, then,
this must be explicitly declared in the function declaration via a
throws SomeException clause.
(yes yes as long as SomeExceptions is a descendant of Throwable or
something like that)
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
|
Because by the time that exceptions were introduced to the development
of C++ the language was already 10 years old with millions of lines of
existing code. The only way to make such a rule work would be by having
the default (no exception specification) be 'nothrow' rather than the
current 'can throw anything'.
Actually experience suggests that exception specifications are flawed
beyond salvation (and they are nowhere near as valuable in Java as the
designers originally expected) in C++ where the existence of templates
and generic programming cause major obstacles to exception
specifications.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Fri Dec 23, 2005 6:42 pm Post subject: Re: why arent C++ exceptions declared in function declarati |
|
|
<mentat79 (AT) gmail (DOT) com> wrote
| Quote: | In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
|
One fundamental reason is to permit, but not require, implementations to
throw exceptions in the case of conditions such as overflow. If function
declarations had to name all possible exceptions, then it would be necessary
to define what exceptions could possibly occur in the case of overflow and
related circumstances, and also to mention those exceptions in every
function that does arithmetic.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
puzzlecracker Guest
|
Posted: Mon Dec 26, 2005 2:13 am Post subject: Re: why arent C++ exceptions declared in function declaratio |
|
|
Francis Glassborow wrote:
| Quote: | In article <1135274202.983557.310930 (AT) g47g2000cwa (DOT) googlegroups.com>,
"mentat79 (AT) gmail (DOT) com" <mentat79 (AT) gmail (DOT) com> writes
Hello,
In Java, if a function body has a throw(SomeException) statement, then,
this must be explicitly declared in the function declaration via a
throws SomeException clause.
(yes yes as long as SomeExceptions is a descendant of Throwable or
something like that)
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
Because by the time that exceptions were introduced to the development
of C++ the language was already 10 years old with millions of lines of
existing code. The only way to make such a rule work would be by having
the default (no exception specification) be 'nothrow' rather than the
current 'can throw anything'.
Actually experience suggests that exception specifications are flawed
beyond salvation (and they are nowhere near as valuable in Java as the
designers originally expected) in C++ where the existence of templates
and generic programming cause major obstacles to exception
specifications.
|
| Quote: | in C++ where the existence of templates
and generic programming cause major obstacles to exception
specifications.
|
That would make java 5.0 suffer the same problem as generic code in C++
does. But, it didn't! It would be valid to assert that exception is
C++ have the same share of usefulness as in Java given non-generic
code.
I agree with aforementioned statement - that programmers never really
learnt how to use exception nor designed system based on latter before
their implementation, thus avoiding them altogether, whereas in Java,
exception specifications existed since the inception.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Dec 28, 2005 12:37 pm Post subject: Re: why arent C++ exceptions declared in function declaratio |
|
|
puzzlecracker wrote:
| Quote: | Francis Glassborow wrote:
In article
[email]1135274202.983557.310930 (AT) g47g2000cwa (DOT) googlegroups.com[/email]>,
"mentat79 (AT) gmail (DOT) com" <mentat79 (AT) gmail (DOT) com> writes
In Java, if a function body has a throw(SomeException)
statement, then, this must be explicitly declared in the
function declaration via a throws SomeException clause.
|
[...]
| Quote: | in C++ where the existence of templates and generic
programming cause major obstacles to exception
specifications.
That would make java 5.0 suffer the same problem as generic
code in C++ does. But, it didn't!
|
That's because, unlike what the original poster claims, Java
exceptions aren't checked. Java only checks exceptions which
don't derive from either RuntimeException or Error. In other
words, errors which should be reported by a return value rather
than an exception anyway. (Java is often more or less forced to
use exceptions here because it doesn't have out parameters.
Sort of a case of trying to correct one mistake with another.)
In practice, I'm not convinced that there is a real conflict
between exception specifications and generic programming, when
exception specifications are used in a reasonable manner.
Basically, the only really useful information in an exception
specification is whether the function throws or not. Or rather,
whether the function guarantees or not that it will not throw as
part of its contractual interface.
| Quote: | It would be valid to assert that exception is C++ have the
same share of usefulness as in Java given non-generic code.
I agree with aforementioned statement - that programmers
never really learnt how to use exception nor designed system
based on latter before their implementation, thus avoiding
them altogether, whereas in Java, exception specifications
existed since the inception.
|
But they don't work any better than those in C++. (In fact,
they actually are less useful, since there is no way in Java to
declare that a function guarantees that it will not throw
anything. In sum, the only useful declaration is not
available.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wij@seed.net.tw Guest
|
Posted: Wed Dec 28, 2005 5:13 pm Post subject: Re: why arent C++ exceptions declared in function declaratio |
|
|
kanze wrote:
| Quote: | In practice, I'm not convinced that there is a real conflict
between exception specifications and generic programming, when
exception specifications are used in a reasonable manner.
Basically, the only really useful information in an exception
specification is whether the function throws or not. Or rather,
whether the function guarantees or not that it will not throw as
part of its contractual interface.
|
Throw specification is not part of the function signature and
can't be in typedef.
| Quote: | It would be valid to assert that exception is C++ have the
same share of usefulness as in Java given non-generic code.
I agree with aforementioned statement - that programmers
never really learnt how to use exception nor designed system
based on latter before their implementation, thus avoiding
them altogether, whereas in Java, exception specifications
existed since the inception.
But they don't work any better than those in C++. (In fact,
they actually are less useful, since there is no way in Java to
declare that a function guarantees that it will not throw
anything. In sum, the only useful declaration is not
available.)
|
I don't use Java, but as mentioned by Andrew Koening, there are many
cases like ++i or a+b with arithmetic types. throw specification is
not easy to happen.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kk_oop@yahoo.com Guest
|
Posted: Sat Dec 31, 2005 1:25 pm Post subject: Re: why arent C++ exceptions declared in function declaratio |
|
|
[email]mentat79 (AT) gmail (DOT) com[/email] wrote:
<snip>
| Quote: |
In C++ the throws declaration is optional.
I would like to know why this is so. Is it just a stylistic issue or
are there deeper reasons related to the C++ programming model?
|
Here's an important consideration. If function f( ) has a "throws"
declaration and f( ) throws an exception not in that declaration,
unexpected(), defined in the C++ standard library, is invoked. The
default behavior of unexpected() is to call terminate().
This is really dangerous if f( ) is using a third party library that
may throw exceptions now or in future releases that the
owner/maintainer of f( ) is not aware of. If one of those exceptions
gets thrown in f( ), and f( ) lets it escape (i.e., does not catch it),
termination will occur. If you are writing any safety critical code
that must not abort, this makes f( ) very fragile, since it must be
aware of every possible exception that can bubble up from any of its
function calls now and forever.
If you do not use an exception specification, such an exception would
just quietly bubble up, hopefully to some generic catch at some higher
level function. Note that if that generic catch were not there, the
program would still abort, but it's far easier to have a generic catch
in one place, than fragile exception specifications all over the place.
- Ken
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|