3 ms·
try/finally in the context of thrown exceptions, sure. But in a language without exceptions (like portable C), we can alternatively define try/finally in terms
by DaiPlusPlus 3y ago
try/finally in the context of thrown exceptions, sure.
But in a language without exceptions (like portable C), we can alternatively define try/finally in terms of a "guaranteed execution" block no matter how control returns from a guarded try-block (be it `return`, `break`, `goto`, `longjmp`, etc).
...basically, a safer form of goto. It's possible to implement a form of this in portable C using only preprocessor macros, but it's messy and can't handle some cases, like longjmp.
- shiroiuma 3y agoI see, very interesting. Personally, I'd prefer to just have a "finally:" label; I've never liked "try", because it seems like extra boilerplate, and forces another level of indentation.
- DaiPlusPlus 3y agoI like how Swift did it: instead of try/finally, they have `defer`, which (when encountered) pushes a new clean-up task to the guaranteed-execution list in the right order: https://www.hackingwithswift.com/new-syntax-swift-2-defer https://www.hackingwithswift.com/new-syntax-swift-2-defer
- shiroiuma 3y agoThat does sound good. It looks like golang has the same type of construct.