4 ms·
How is finally() ever useful in any language? What is the difference between try{ #code that can throw errors } catch { #handle error } finally
by themoonisachees 3y ago
How is finally() ever useful in any language? What is the difference between
try{
#code that can throw errors
} catch {
#handle error
} finally {
#code
}
And
try{
#throw err
}catch {
#handle
}
#code...
Surely in pretty much all languages the two are equivalent, so what is finally's purpose as a token?
The trick in this post is different because for one bash doesn't have finally, and even if it did you couldn't use it to catch all of a script's errors.
- anuragsoni 3y ago`finally` will also cover scenarios where the an exception is raised from within catch, or if the catch block doesn't handle an exception that's raised in the `try` block.
- maskros 3y agoIf "handle error" in the catch involves rethrowing the error, or another error, or returning an error code from the function, the "finally" block is _still_ executed. Here's a "fun" example in Javascript: function foo() { try { return 42 } finally { return "boo!" } }