如何处理 Visual Studio C++ 中的访问冲突? [英] How can I handle an access violation in Visual Studio C++?
问题描述
通常访问冲突会终止程序,我无法使用 try
和 catch
捕获 Win32 异常.有没有办法让我的程序保持运行,即使发生访问冲突?最好我想处理异常并向用户显示发生了访问冲突.
Usually an access violation terminates the program and I cannot catch a Win32 exception using try
and catch
. Is there a way I can keep my program running, even in case of an access violation? Preferably I would like to handle the exception and show to the user an access violation occurred.
我希望我的程序非常健壮,甚至可以防止编程错误.我真正想要避免的是程序终止,即使以某些损坏的状态为代价.
I want my program to be really robust, even against programming errors. The thing I really want to avoid is a program termination even at the cost of some corrupted state.
推荐答案
在 Windows 中,这称为结构化异常处理 (SEH).有关详细信息,请参阅此处:
In Windows, this is called Structured Exception Handling (SEH). For details, see here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx
实际上,您可以注册以在发生异常时获得回调.出于显而易见的原因,您不能对每个例外都这样做.
In effect, you can register to get a callback when an exception happens. You can't do this to every exception for obvious reasons.
使用 SEH,您可以检测到很多异常,包括访问冲突,但不是全部(例如双栈错误).即使存在可检测的异常,也无法确保异常发生后 100% 的稳定性.但是,通知用户、记录错误、向服务器发送消息并正常退出可能就足够了.
Using SEH, you can detect a lot of exceptions, access violations included, but not all (e.g. double stack fault). Even with the exceptions that are detectable, there is no way to ensure 100% stability after the exception. However, it may be enough to inform the user, log the error, send a message back to the server, and gracefully exit.
这篇关于如何处理 Visual Studio C++ 中的访问冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!