Article View: comp.std.c++
Article #2930Re: throw declarations
From: Steve Clamage
Date: Thu, 14 Dec 1995 18:25
Date: Thu, 14 Dec 1995 18:25
49 lines
3036 bytes
3036 bytes
In article 5zlIdu73cDB@faerber.muc.de, claus@faerber.muc.de (Claus Andre Faerber) writes: >clamage@Eng.sun.com (08 Dec 95): > >> The current rule, related to the original question, is that function f4 >> is OK, and an error results only if g actually exits via an exception. >> The alternative would be to require that every function called have an >> exception specification at least as restrictive as the calling function. > >.... or a try-catch statement, which catches other exceptions and must not >re-throw them. Of course, but that is in general the case anyway. >> Such a rule would make exceptions specifications completely useless. > >If a function can throw an exception although it was declared as throw(), >what's the point of having such a throw declaration at all ?? >Wether a function can throw an exception or not could be documented with >the meaning of the function parameters and the use of the function. >Why does the compiler need to be told this, if it doesn't do anything to >prevent functions from behaving differently ?? A throw specification is a promise from the programmer to the compiler (and to users of the code) that the function will not throw any other exceptions. The compiler is allowed to generate code on that assumption, but for one thing: the current version of unexpected() must be called in the event of a disallowed exception escaping the function. So the compiler has one extra chore: at the point where an exception escapes from a function having an exception specification, the compiler might need to add a test for validity. You actually have an additional option. If a function f calls another function g which has a less restrictive throw specification, function f can install an unexpected-handler that converts a disallowed exception into one which is allowed by f's throw specification. That would be the moral equivalent of f catching the exception itself, but it allows you to write function f without cluttering it with unrelated catch clauses; a single version of "unexpected" could serve the needs of a collection of functions. Or you could let the program terminate, which is the default action anyway. --- Steve Clamage, stephen.clamage@eng.sun.com [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<4apq1u$7tp@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<5zlIdu73cDB@faerber.muc.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: throw declarations Date: 14 Dec 1995 18:25:16 GMT Organization: Sun Microsystems Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4apq1u$7tp@engnews1.eng.sun.com> References: <5zlIdu73cDB@faerber.muc.de> Reply-To: clamage@Eng.sun.com NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 2224 X-Lines: 45 Originator: clamage@taumet