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(…
C++ says “We have try... finally at home”
81–90 of 152 posts
Re: C++ says “We have try... finally at home”
#82Earlier 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.
Re: C++ says “We have try... finally at home”
#83Earlier 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.
Re: C++ says “We have try... finally at home”
#84Earlier 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?
Re: C++ says “We have try... finally at home”
#85The 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.
Re: C++ says “We have try... finally at home”
#86Earlier 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?
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”
#87Earlier 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.
Re: C++ says “We have try... finally at home”
#88Earlier 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.
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”
#89Earlier 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.
Re: C++ says “We have try... finally at home”
#90In other words: Footgun #17421 Exhibit A.
In Java the following is perfectly valid:
try { throw new IllegalStateException("Critical error"); } finally { return "Move along, nothing to see here"; }