I'm having some trouble understanding how Exceptions thrown from constructors work...
Let's say I have a class like this:
class A {
public:
A() {
if( error )
throw MyExp;
}
}
If I try to define a variable of type "A" within a try {} catch {}
block, I run into problems with scope:
int main() {
try {
A a_definition;
} catch( Exp error ) {
std::cout << error.message << endl;
}
a_definition; // out of scope now...
}
Wrapping the whole program in a try/catch block doesn't seem very appealing to me! And while I could do something like this:
A *a_ptr = NULL;
try {
a_ptr = new A;
} catch( Exp error ) { // so on...
It doesn't look very clean; I don't need a pointer, other than for the work-around.
Is there an easy way to do this?