KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a class whose constructor might throw an exception. class A { A() { /* throw exception under certain circumstances */ } }; I would like to catch this exception in the client for a stack-allocated instance. But I find myself forced to extend the try block at least as far as the instance must be alive. try { A a; do_something(a); } catch { // ... } Now this obviously becomes a problem when the try block is too large to track down the source of the exception: try { A a1; A a2; do_something(a1, a2); } catch { // Who caused the exception? } What would I do to avoid the situation? UPDATE : It seems I hadn't explained the problem very well: For obvious reasons, I want to have the try block spanning as little code as necessary (that is, only the construction). But that creates the problem that I can't use the objects afterwards because they have moved 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);
Tags (comma-separated)
Save Edits
Cancel