Re: Proposal: A block between "try" and "catch".
On 5 Jun., 11:22, "Adam H. Peterson" <alpha.eta...@gmail.com> wrote:
I have an exception handling proposal for the language.
Sometimes I want to check a small block of code for an exception, but
I want the recovery point to be lower in the function than I want the
try block to guard against. For example, I may have something like:
try {
Object ob("data"); // May throw range_error
// This may also throw range_error,
// but I don't want to catch this one
do_something_else(ob, "other data");
} catch (range_error e) {
// handle the failed construction of ob.
}
What is wrong with:
Object ob("data"); // May throw range_error
try {
// This may also throw range_error,
// but I don't want to catch this one
do_something_else(ob, "other data");
} catch (range_error e) {
// handle the failed construction of ob.
}
?
This handles exactly what you want to handle.
I'd like to propose an extension to the try{} syntax that allows
specifying a code region inside the try{} where exceptions aren't
caught. In the interest of not introducing new keywords, I'd suggest
a syntax something like this:
try {
// Code where exceptions are caught
T var;} catch void {
// The catch blocks below don't apply to this code.
// However, it is an extension of the above scope,
// so var is still visible here.
var.still_visible();} catch (E) {
// This block is entered if T() throws E,
// but not if still_visible() throws E.
// That exception would propagate.
}
Anyway, I've had to work around this usage scenario frequently enough
that I think it might be worthwhile to extend the language to handle
it. But what do you all think?
I believe this is obfuscation. A catch block that sometimes does not
catch anything is not something I would recommend.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]