refactor exception and IO close.