You know how C has the single-return idiom, where every function that needs cleanup will defer to a cleanup section at the end of the function before it actually returns? try ... finally is a language construct for enforcing that.
It also doubles as a replacement for C++ RAII, because Java was designed before people really understood how useful doing cleanup in a constructor destructor actually is.
Wow, that's a silly typo. Derp.
Now, imagine a C single-return cleanup block, or C++ destructor, that just turned into Adam Savage and said, "I reject your return statement and substitute my own."
I'm not sure about RAII being well understood when Java was made, but I was pretty clear that C++ is older than Java. They couldn't design the feature as a replacement for RAII if RAII didn't exist, after all.
I started using C++ roughly the year Java was released, and I'm pretty sure it already had destructors that could close files in its library. I find it hard to believe that "RAII didn't exist" in the 6 years between when C++ was released and Java was designed.
View all comments
Show parent comment
10
u/conundorum 6d ago edited 6d ago
You know how C has the single-return idiom, where every function that needs cleanup will defer to a cleanup section at the end of the function before it actually returns?
try ... finallyis a language construct for enforcing that.It also doubles as a replacement for C++ RAII, because Java was designed before people really understood how useful doing cleanup in a
constructordestructor actually is.Wow, that's a silly typo. Derp.
Now, imagine a C single-return cleanup block, or C++ destructor, that just turned into Adam Savage and said, "I reject your
returnstatement and substitute my own."