Search code examples
scalaexception

Is `finally` block executed in case there is `return` inside a `try` or a `catch` block?


Using a try-catch-finally construction to retrieve a database record, it seems that I need to return a value inside a try block in case everything was fine (as in case of an exception the end of the function is not meant to be reached). But If I return inside try, is finally code going to be reached (to close the connection etc.)?


Solution

  • Yes,

    The result of the try/catch expression will be the last line of either the try or catch block, but the finally block will always execute no matter what