Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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();
    }


Yep, 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
    };




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: