I have a class whose constructor can throw an exception.
class A { A() { } };
I would like to catch this exception in the client for the instance allocated on the stack. But I have to stretch the try block, at least as far as the instance should be alive.
try { A a; do_something(a); } catch {
Now this becomes a problem when the try block is too large to track the source of the exception:
try { A a1; A a2; do_something(a1, a2); } catch {
What would I do to avoid the situation?
UPDATE
It seems that I did not explain this problem very well: for obvious reasons, I want the try block to cover as little code as possible (i.e. just the construction).
But this creates a problem, because of which I cannot use objects after that, because they went out of scope.
try { A a1; } catch { // handle a1 constructor exception } try { A a2; } catch { // handle a2 constructor exception } // not possible do_something(a1, a2);
Jo so
source share