SPARK-1656: Fix potential resource leaks

JIRA: https://issues.apache.org/jira/browse/SPARK-1656
Hm I'm still not so sure about having the nested `try`s. Doesn't this also affect other changes made in this PR? I think it might be OK to just keep the close in both `try` and `catch` and have only 1 level of `try-catch`. It's already strictly better than before (maybe others disagree).