Optimization: moving try catch outside of loop.