Catch StackOverflowException for recursive function