vc++ vs std::current_exception()
От: _niko_ Россия  
Дата: 16.04.19 09:36
Оценка:
Наткнулся я тут на интересную багу (как мне во всяком случае кажется) в роботе с исключениями у MSVC

  Код
#include <string>
#include <iostream>
#include <exception>

void print_except(std::exception_ptr exc)
{
    const int uncaught_except_count{ std::uncaught_exceptions() };

    if (exc)
    {
        try
        {
            std::rethrow_exception(exc);
        }
        catch (const std::exception& std_exc)
        {
            std::cout << "[uncaught-except: " << uncaught_except_count << "] except-what: " << std_exc.what() << std::endl;
        }
        catch (...)
        {
            std::cout << "[uncaught-except: " << uncaught_except_count << "] <unknown-except>" << std::endl;
        }
    }
    else
    {
        std::cout << "[uncaught-except: " << uncaught_except_count << "] <unknown-except: null-except-or-exists-uncaught-exceptions>" << std::endl;
    }
}

class ExceptGuard
{
public:

    ExceptGuard(std::string message)
        : m_message(std::move(message))
    {}

    ~ExceptGuard() noexcept(false)
    {
        try
        {
            throw std::runtime_error(m_message);
        }
        catch (...)
        {
            print_except(std::current_exception());
        }
    }

private:

    std::string m_message;
};

int main()
{
    try
    {
        const ExceptGuard except_guard{ "except_1" };

        throw std::runtime_error("except_2");
    }
    catch (...)
    {
        print_except(std::current_exception());
    }

    return 0;
}

  Out: vc++
[uncaught-except: 1] <unknown-except: null-except-or-exists-uncaught-exceptions>
[uncaught-except: 0] except-what: except_2

  Out: clang & gcc
[uncaught-except: 1] except-what: except_1
[uncaught-except: 0] except-what: except_2
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.