StackOverFlow 异常,我的函数的替代方案?
我创建了一个程序,在某些时候必须登录某个网站。如果出现问题,我希望它等待一段时间,比如 2 秒,然后重试。所以该函数看起来像这样:
public void log_in ()
{
try
{
//login code
}
catch
{
Thread.Sleep(2000);
log_in();
}
}
但是这段代码开始时不时地给我 StackOverFlowExeptions 。有谁知道如何解决这个问题?
I've created a program that at some point has to log in to some website. If something goes wrong, I want it to wait for some time, say 2 seconds, and then try again. so the function looks something like this:
public void log_in ()
{
try
{
//login code
}
catch
{
Thread.Sleep(2000);
log_in();
}
}
But this code started to give me StackOverFlowExeptions every now and then. Does anyone know how to solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会收到堆栈溢出异常,因为每次调用
log_in()
时都会将该函数添加到堆栈中。这个这个:You're getting a stack overflow exception because every time you call
log_in()
you're adding the function to the stack. This this:这是因为,如果代码根本无法登录,它每次都会在堆栈上创建新的函数调用。下一个代码可以工作:
但总的来说,您不应该在多次向用户/管理员报告错误后尝试无休止地登录。
what's because if code can't login at all it keeps creating new function call each time on the stack. Next code would work:
But overall you shouldn't try to login endlessly, after several time report error to user/admin.