Catch recursion depth exception for file IO