A try
block is an expression. The finally
runs as part of the evaluation of that expression. The expression on the right-hand side of an assignment is evaluated before the assignment takes place. From this we conclude that the finally
block, as part of that evaluation, runs before the assignment takes place.
Yes, it runs in the same thread. I have no concrete reference for that other than knowing that it would be a very unfortunate language feature if it did not (and rather difficult to implement anyways).
Your use of the word "returned" is confusing. Nothing is being "returned"; an expression is being evaluated and its resulting value is being assigned to something.
As for the details of the expression itself, from the Scala language specification, section 6.22:
A try expression ...
try { b } finally e
... evaluates the block b
. If
evaluation of b
does not cause an exception to be thrown, the expression e
is evaluated.
If an exception is thrown during evaluation of e
, the
evaluation of the try expression is aborted with the thrown exception.
If no exception is thrown during evaluation of e
, the result of b
is
returned as the result of the try expression. If an exception is
thrown during evaluation of b
, the finally block e
is also evaluated. If another exception e
is thrown during evaluation of e
,
evaluation of the try expression is aborted with the thrown exception.
If no exception is thrown during evaluation of e
, the original
exception thrown in b
is re-thrown once evaluation of e
has completed.
The block b
is expected to conform to the expected type of the try
expression. The finally expression e
is expected to conform to type
Unit
.
A try expression:
try { b } catch e1 finally e2
is a shorthand for
try { try { b } catch e1 } finally e2
Further details can be found in that section.