I’m trying to make my C++ code exception-safe, and got a problem that neither asking friends nor searching web will help.
On my understanding, when creating an object with constructor potentially throw an exception, the code for creation needs to be enclosed with try block and exception handling is done in catch(){}.
If the creation is heap-based(e.g. newed with default allocator), I can place exception handling code near the creation like this:
void f() {
// work unrelated to Hoge object here
try {
Hoge *pHoge = new Hoge(); // could throw an exception
} catch(HogeException& ex) {
// handle exception
}
// rest of work here
}
However, if the creation is stack-based, I can’t find ways to do that and resort to code like below due to the scope of try block:
void g() {
// work unrelated to Hoge object here
try {
Hoge hoge; // could throw an exception
// rest of work here
} catch(HogeException& ex) {
// handle exception
}
}
If // rest of work code above is large, the distance of locations between object creation and exception handling could be long, decreasing code readability…
I prefer the exception handling code is near object creation(and maybe that is one of the concepts of try–catch structure). Is there any solutions?
Delegate the
// rest of workto a helper function, and pass aHoge&to that function:Incidentally,
Hoge hoge();doesn’t do what you think it does. You probably think that you are declaring an object namedhogeof typeHoge, and initializing it by calling the default constructor. What you’re actually doing is declaring a function namedhogewhich takes no parameters and returns aHogeby-value. I’ve fixed this in my code above.Edit Indeed, as suggested by @LightnessRacesInOrbit, the construction of the
Hogeobject can take place in the deferral function as well, such as with: