analogous Try block to try/finally block in scala

functional-programming, scala

Solution

Given that an exception inside a `Try` simply creates a `Failure` value (as opposed to transferring control to an outer catch block when using `try`), the code in your original `finally` block would just need to be executed after the `Try`. In other words, this will do:

timer.start()
val result = Try{
  doThis()
}
timer.cancel()
result

As far as I know there is no built-in shortcut that would allow to avoid capturing `result` just to return it as is.

Problem

What is the analogous in manners of scala `Try` to: ``` timer.start() try { doThis() } finally { timer.cancel() } ```

Original source

Related problems