Thursday 15 May 2014

scala - How and when exactly does a finally block execute? -



scala - How and when exactly does a finally block execute? -

consider next scala code (similar code examples work java)

val = seek "hello" grab { case e:exception => throw new exception("from catch") } println("done")

finally executes after "hello" returned. when executed? run after homecoming before assigned a? (it seems that) execute in same thread assignment?

a try block expression. finally runs part of evaluation of expression. look on right-hand side of assignment evaluated before assignment takes place. conclude finally block, part of evaluation, runs before assignment takes place.

yes, runs in same thread. have no concrete reference other knowing unfortunate language feature if did not (and rather hard implement anyways).

your utilize of word "returned" confusing. nil beingness "returned"; look beingness evaluated , resulting value beingness assigned something.

as details of look itself, scala language specification, section 6.22:

a seek look ...

try { b } e

... evaluates block b. if evaluation of b not cause exception thrown, look e evaluated.

if exception thrown during evaluation of e, evaluation of seek look aborted thrown exception. if no exception thrown during evaluation of e, result of b returned result of seek expression. if exception thrown during evaluation of b, block e evaluated. if exception e thrown during evaluation of e, evaluation of seek look aborted thrown exception. if no exception thrown during evaluation of e, original exception thrown in b re-thrown 1 time evaluation of e has completed. block b expected conform expected type of seek expression. look e expected conform type unit.

a seek expression:

try { b } grab e1 e2

is shorthand for

try { seek { b } grab e1 } e2

further details can found in section.

scala jvm

No comments:

Post a Comment