Live data from Hacker News

C++ says “We have try... finally at home”

devblogs.microsoft.com

81–90 of 152 posts

Re: C++ says “We have try... finally at home”

#81
post #25

Earlier quoted context omitted.

> Most of the languages that have finally clauses also have destructors. Hm, is that true? I know of finally from Java, JavaScript, C# and Python, and none of them have proper destructors. I mean some of them have object finalizers which can be used to clean up resources whenever the garbage collector comes around to collect the object, but those are not remotely similar to destructors which typically run determinist…

In C# the closest analogue to a C++ destructor would probably be a `using` block. You’d have to remember to write `using` in front of it, but there are static analysers for this. It gets translated to a `try`–`finally` block under the hood, which calls `Dispose` in `finally`. using (var foo = new Foo()) { } // foo.Dispose() gets called here, even if there is an exception Or, to avoid nesting: using var foo = new Foo(…

That approach doesn't allow you to move the file into some long lived object or return it in the happy path though, does it?

Re: C++ says “We have try... finally at home”

#82

Earlier quoted context omitted.

In C# the closest analogue to a C++ destructor would probably be a `using` block. You’d have to remember to write `using` in front of it, but there are static analysers for this. It gets translated to a `try`–`finally` block under the hood, which calls `Dispose` in `finally`. using (var foo = new Foo()) { } // foo.Dispose() gets called here, even if there is an exception Or, to avoid nesting: using var foo = new Foo(…

Java's is try (var foo = new Foo()) { } // foo.close() is called here. I like the Java method for things like files because if the there's an exception during the close of a file, the regular `IOException` block handles that error the same as it handles a read or write error.

What do you do if you wanna return the file (or an object containing the file) in the happy path but close it in the error path?

Re: C++ says “We have try... finally at home”

#83
post #61

Earlier quoted context omitted.

It has been up with the incorrect title for over 7 hours now. That's most of the Hacker News front-page lifecycle. The system for correcting bad automatic editorialisation clearly isn't working well enough.

Oh, come on man! These are trivial bugs . Whoever noticed it first should have sent the email to the mods. I did it before i posted my previous comment and i now see that the title has been changed appropriately.

7. hours.

Re: C++ says “We have try... finally at home”

#84
post #81

Earlier quoted context omitted.

In C# the closest analogue to a C++ destructor would probably be a `using` block. You’d have to remember to write `using` in front of it, but there are static analysers for this. It gets translated to a `try`–`finally` block under the hood, which calls `Dispose` in `finally`. using (var foo = new Foo()) { } // foo.Dispose() gets called here, even if there is an exception Or, to avoid nesting: using var foo = new Foo(…

That approach doesn't allow you to move the file into some long lived object or return it in the happy path though, does it?

As someone coming from RAII to C#, you get used to it, I'd say. You "just" have to think differently. Lean into records and immutable objects whenever you can and IDisposable interface ("using") when you can't. It's not perfect but neither is RAII. I'm on a learning path but I'd say I'm more productive in C# than I ever was in C++.

Re: C++ says “We have try... finally at home”

#85
post #2

The submitted title is missing the salient keyword "finally" that motivates the blog post. The actual subtitle Raymond Chen wrote is: "C++ says “We have try…finally at home.”" It's a snowclone based on the meme, "Mom, can we get ? No, we have at home." : https://www.google.com/search?q=%22we+have+x+at+home%22+meme In other words, Raymond is saying... "We already have Java feature of 'finally' at home in the C++ refri…

That's why you shouldn't use memes in the titles of technical articles. The intelligibility of your intent is vastly reduced.

The title of the blog post is perfectly intelligible. It becomes unintelligible when you remove random words from it.

Re: C++ says “We have try... finally at home”

#86
post #82

Earlier quoted context omitted.

Java's is try (var foo = new Foo()) { } // foo.close() is called here. I like the Java method for things like files because if the there's an exception during the close of a file, the regular `IOException` block handles that error the same as it handles a read or write error.

What do you do if you wanna return the file (or an object containing the file) in the happy path but close it in the error path?

You'd write it like this

    void bar() {
      try (var f = foo()) {
        doMoreHappyPath(f);
      }
      catch(IOException ex) {
        handleErrors();
      }
    }

    File foo() throws IOException {
      File f = openFile();
      doHappyPath(f);
      if (badThing) {
        throw new IOException("Bad thing");
      }
      return f;
    }
That said, I think this is a bad practice (IMO). Generally speaking I think the opening and closing of a resource should happen at the same scope.

Making it non-local is a recipe for an accident.

*EDIT* I've made a mistake while writing this, but I'll leave it up there because it demonstrates my point. The file is left open if a bad thing happens.

Re: C++ says “We have try... finally at home”

#87
post #69
post #13

Earlier quoted context omitted.

I'm curious about the actual origin now, given that a quick search shows only vague references or claim it is recent, but this meme is present in Eddie Murphys "Raw" from 1987, so it is at least that old.

Sounds like a perfect fit for some Deep Research. Edit: A deep research run by Gemini 3.0 Pro says the origin is likely to be stand-up comedy routines between 1983–1987 and particularly mentions Eddie Murphy, and the 1983 socioeconomic precursor "You ain't got no McDonald's money" in Delirious (1983) culminating in the meme from in Raw (1987). So Eddie might very well be the original origin.

[deleted]

Re: C++ says “We have try... finally at home”

#88
post #59

Earlier quoted context omitted.

A writable file closing itself when it goes out of scope is usually not great, since errors can occur when closing the file, especially when using networked file systems. https://github.com/isocpp/CppCoreGuidelines/issues/2203

Any fallible cleanup function is awkward, regardless of error handling mechanism.

Java solved it by having exceptions be able to attach secondary exceptions, in particular those occurring during stack unwinding (via try-with-resources).

The result is an exception tree that reflects the failures that occurred in the call tree following the first exception.

Re: C++ says “We have try... finally at home”

#89
post #85

Earlier quoted context omitted.

That's why you shouldn't use memes in the titles of technical articles. The intelligibility of your intent is vastly reduced.

The title of the blog post is perfectly intelligible. It becomes unintelligible when you remove random words from it.

It still didn't make sense to me with all the words. The way it finally made sense was seeing the formatting is "We have `try...finally` at home".

Re: C++ says “We have try... finally at home”

#90

In other words: Footgun #17421 Exhibit A.

What the blog doesn't mention is how try finally can mess up your control flow.

In Java the following is perfectly valid:

try { throw new IllegalStateException("Critical error"); } finally { return "Move along, nothing to see here"; }

Post reply on HN