捕获没有 try/catch 抛出的异常

发布于 2024-11-06 22:15:21 字数 180 浏览 2 评论 0原文

我记得读过有关如何在不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-11-13 22:15:21

在 C++ 中,取消引用空指针会导致未定义的行为,这并不一定意味着引发异常。例如,在 Unix 系统上,会发出 SIGSEGV 信号。

在 Windows 上,访问冲突会引发 SEH 例外。 SEH异常与C++异常不同;它们是使用 __try/__ except< 处理的/code>语句(与 try/catch 语句相对)。未处理的 SEH 异常会调用未处理的异常筛选器,您可以使用 SetUnhandledExceptionFilter

#include <Windows.h>
#include <iostream>

LONG WINAPI MyFilter(EXCEPTION_POINTERS * /*ExceptionInfo*/)
{
    std::cout << "An uncaught exception was detected!\n";

    // Ultimately causes Windows Error Reporting to be invoked.
    // Use EXCEPTION_EXECUTE_HANDLER to silently terminate the application.
    return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
    SetUnhandledExceptionFilter(MyFilter);
    *(char volatile *)0 = 0;
}

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 to try/catch statements). An unhandled SEH exception invokes unhandled exception filter, which you can set using SetUnhandledExceptionFilter.

#include <Windows.h>
#include <iostream>

LONG WINAPI MyFilter(EXCEPTION_POINTERS * /*ExceptionInfo*/)
{
    std::cout << "An uncaught exception was detected!\n";

    // Ultimately causes Windows Error Reporting to be invoked.
    // Use EXCEPTION_EXECUTE_HANDLER to silently terminate the application.
    return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
    SetUnhandledExceptionFilter(MyFilter);
    *(char volatile *)0 = 0;
}
糖果控 2024-11-13 22:15:21

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.

嘦怹 2024-11-13 22:15:21

这是操作系统实现细节,取消引用空指针不会生成 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文