This slays me.
The try, catch, and finally blocks should be just one lexical scope.
This slays me.
The try, catch, and finally blocks should be just one lexical scope.
If, as is kind of the point of RAII, your constructors do some initialization and that initialization may fail, then:
If you have a single object that may fail to construct then maintaining it in scope for the catch block doesn't make sense; it's not initialized.
If you have several, then you still can't maintain them in scope, because you don't know which ones are actually valid.
Yes there are other patterns that wouldn't have this issue (acquiring resources outside of the constructor), but then you can just declare your objects outside of the try-catch block.
Ignoring the syntax for a moment, I want scoping to behave like this pseudo-code. So that the catch and finally blocks are lexically nested within the parent try block.
try {
OutputStream out = ...
...
// Must be at end of try block
catch ExceptionA, Exception B { ... }
catch Exception C { ... }
finally { ... }
} // end of try
Maybe even allow catch and finally to allow single expression in addition to blocks. Just like with if/then/else.* it's not an error to have an unbound name in scope; just referencing it at runtime is an error. * there is a reasonable default; anything can be set to None
This doesn't hold in C++. If your class throws during its initialization, you can't really do much (even default initialization of members may not work). Reassignment may not be possible if it was declared const.
And there's still the issue of 'which objects successfully initialized'?
I just want 'out' visible to both catch and finally.