I'm looking for the equivalent semantics to the popular Try-Finally exception cleanup pattern, e.g. Why use try … finally without a catch clause?
The idea is that you have cleanup steps that need to happen regardless of whether the code succeeds or fails, but the cleanup code shouldn't interfere with reporting and handling the error. Something still went wrong and the exception should still propagate.
I would like to write something like this:
O TempFile:(/NEW:/WRITE:/STREAM)
U TempFile
L +LockName
TRY {
...code that uses TempFile and may throw an error
} FINALLY {
//Be sure to delete file whether we have an error or not
O TempFile:(/DELETE)
C TempFile
//Be sure to release lock
L -LockName
}
... Rest of code that should only execute if there was no error
But the TRY...CATCH syntax in ObjectScript does not support the FINALLY clause.
In particular, it's important that both of these things normally done by finally blocks hold true:
I can't simply use a regular TRY...CATCH block because CATCH will eat the exception and stop the correct error context from being passed up the chain. Maybe there is a way to re-throw the original exception without messing up the error context?
you can throw caught error, and it will be original error, with original place of error and anything else, so Try-Finally may looks like below.
try {
// some code that could be with errors
} catch ex {
}
// finally
throw:$g(ex) ex
// rest code that can't execute if was error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With