捕获没有 try/catch 抛出的异常
我记得读过有关如何在不使用 try/catch 的情况下捕获异常的内容。基本上,当通过取消引用空指针引发异常(例如“未处理的异常”)时,即使没有为该异常编写 try/catch,也会触发一个进程。我相信它与您编写然后包含在代码中的顶级库有关。不幸的是,关于这种方法的文档似乎不存在,但我以前见过/听说过这种方法。有人可以解释一下这是如何完成的吗?
I recall reading about how exceptions could be caught without the use of try/catch. Basically when an exception is thrown such as "Unhandled exception" via derefencing a null pointer, there would be a process that is triggered even when a try/catch is not coded for the exception. I believe it had something to do with a top level library that you write then include in your code. Unfortunately documentation on such a method seems to be non-existent but I have seen/heard of such a method being done before. Could someone please explain how this is done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中,取消引用空指针会导致未定义的行为,这并不一定意味着引发异常。例如,在 Unix 系统上,会发出 SIGSEGV 信号。
在 Windows 上,访问冲突会引发 SEH 例外。 SEH异常与C++异常不同;它们是使用
__try/__ except< 处理的/code>
语句(与
try/catch
语句相对)。未处理的 SEH 异常会调用未处理的异常筛选器,您可以使用SetUnhandledExceptionFilter
。In C++, dereferencing a null pointer causes undefined behavior, which does not necessarily imply that an exception is thrown. On Unix systems, for example, a SIGSEGV signal is raised instead.
On Windows, access violations raise a SEH exception. SEH exceptions are not the same as C++ exception; they are handled using
__try/__except
statements (as opposed totry/catch
statements). An unhandled SEH exception invokes unhandled exception filter, which you can set usingSetUnhandledExceptionFilter
.您可能会想到 Windows 结构化异常处理< /a> 机制。由于支持完整的 C++ try/catch 语义,我个人还没有找到任何合理的用例。
You may be thinking of the Windows Structured Exception Handling mechanism. With full C++ try/catch semantics supported, I personally haven't found any reasonable use case for it.
这是操作系统实现细节,取消引用空指针不会生成 C++ 异常。在生成 SEH 异常的 Windows 上,MSVC 编译器允许您使用非标准 __try 和 __ except 关键字处理它们。在生成信号的 *nix 上,您可以使用 signal.h 头文件中的函数。
That's an operating system implementation detail, dereferencing a null pointer doesn't generate a C++ exception. On Windows that generates an SEH exception, the MSVC compiler lets you handle them with the non-standard __try and __except keywords. On *nix that generates a signal, you use the functions in the signal.h header file.