C++,忽略异常并继续代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7653736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:17:16  来源:igfitidea点击:

C++, ignore exception and continue code?

c++exceptiontry-catch

提问by user788171

Is there a way allow execution of a bad block of code after an exception of thrown?

有没有办法允许在抛出异常后执行坏代码块?

Presently, my code has an while loop that runs continuously. The code inside this while loop sometimes throws a vector out of range error. I have been unable to track down the cause of this particular exception, but ultimately, it doesn't matter much because the code inside the while loop does the same thing over and over again and the next iteration does not depend on the previous iteration in any way.

目前,我的代码有一个持续运行的 while 循环。此 while 循环中的代码有时会抛出向量超出范围错误。我一直无法追踪这个特定异常的原因,但最终,这并不重要,因为 while 循环中的代码一遍又一遍地做同样的事情,下一次迭代不依赖于之前的迭代反正。

This, after the code within the while loop crashes, I would like it to start again from the top of the while statement.

这一点,在 while 循环中的代码崩溃后,我希望它从 while 语句的顶部重新开始。

Is there a way to accomplish this in C++? try/catch doesn't seem to work in this situation.

有没有办法在 C++ 中实现这一点?在这种情况下,try/catch 似乎不起作用。

Additional Info: I would love to just take the code within the while loop, make it into its own executable, and put the while loop into a bash script, but there's some data each iteration requires that remains static and it takes too much time to re-load that data each time so I am forced to do my infinite while loop within C++

附加信息:我很想将 while 循环中的代码放入自己的可执行文件中,然后将 while 循环放入 bash 脚本中,但是每次迭代都需要一些数据保持静态,这需要太多时间每次都重新加载该数据,因此我被迫在 C++ 中执行无限 while 循环

回答by driis

You just need to catch the exception inside the while loop:

您只需要在 while 循环中捕获异常:

while(true) 
{
    try 
    {
          // your code
    }
    catch (Exception e) { /* Please, at least do some logging or other error handling here*/ }
}   

回答by David Rodríguez - dribeas

The first thing that you should do is debugthe code, for that you can probably run the code inside a debugger and diagnose what the problem is. Pushing the problem under the rug will not make it go away, and the program will still be buggy.

您应该做的第一件事是调试代码,因为您可以在调试器中运行代码并诊断问题所在。把问题推到地毯下不会让它消失,程序仍然会出错。

If on the other hand, the issue is with something that is truly exceptionalbut feasible (consider opening a file, sending a packet over the network, anything that could potentially fail, but is not expected to --as compared to something that should neverhappen), the try/catchapproach should work.

另一方面,如果问题出在真正特殊但可行的事情上(考虑打开文件,通过网络发送数据包,任何可能会失败但预计不会失败的事情——与永远不应该发生的事情相比)发生),该try/catch方法应该有效。

回答by Code Monkey

if you could possibly post a snippet of code, we all could help you more. but in general you should always have some sort of error handling whether it be a try{}catch{} or just checking a variable like:

如果您可以发布一段代码,我们都可以为您提供更多帮助。但总的来说,无论是 try{}catch{} 还是仅检查变量,您都应该始终进行某种错误处理,例如:

while(true)
{

if(flag == "Error")
{
//error handle
}

else
{
//continue with code execution
}

}

hope you get this problem solved!

希望你能解决这个问题!