5 ms·
> In C++ I find it quite infuriating that try{} opens up a new lexical scope, which means you can't construct something, check for errors and move on You techn
by mostlylurks 3y ago
> In C++ I find it quite infuriating that try{} opens up a new lexical scope, which means you can't construct something, check for errors and move on
You technically can with a small workaround (demonstrated below), though I personally wouldn't use this approach.
void foo() {
auto value = [] {
try {
return Foo();
} catch (...) {
// Something here
}
}();
value.do_something();
}
- grumbel 3y agoYep, I had to resort to that a few times. With a bit of syntactic sugar that could be turned into something like: auto value = try Foo("will fail") catch(...) { return Foo("will work"); // or throw something else };