最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Why is std::terminate handler not called if exceptions are disabled? - Stack Overflow

programmeradmin2浏览0评论

The default std::terminate_handler calls std::abort. std::terminate is called in the case of an unhandled exception, or it can be called manually.

If I provide a terminate handler then it's only called if exceptions are enabled, but I don't see why this should be the case, and there's nothing in the documentation about it being disabled when exceptions are disabled. I don't see the connection between the terminate handler and exceptions, or why it's required. The terminate handler says "do this before aborting".

I don't know why it's disabled with exceptions disabled. Here is a question I asked earlier about this happening to me, and here is a reproducible example I made that shows that the terminate handler is no longer called when exceptions are disabled.

The above is the case on MSVC, and I don't know if it happens also with other compilers.

There is a question from 2018 that says:

std::terminate with disabled exceptions (MSVC implementation)

As it turns out, msvc has dummy implementation for terminate function when _HAS_EXCEPTIONS macro is not set:

    inline void __CRTDECL terminate() _NOEXCEPT
    {   // handle exception termination
    }

So it's just the case with MSVC, but is this still the case in 2025? That std::terminate is just an empty function when exceptions are disabled?

发布评论

评论列表(0)

  1. 暂无评论