Move the codes out of the finally block

Found there were complex codes in the `finally` block. It's a bad practice because:
* The `return` statement in the `finally` block will swallow the exception.
* The codes in the finally block will continue to run even if any exception happens in the `try` block. If the `finally` block contains complex codes, they may continue to run for long time or throw an exception which will override the initial one. It will make us hard to debug and find the real problem.

Therefore I moved them out of the `finally` block in this PR.