Yury Finchenko
Yury Finchenko

Reputation: 1065

std::rethrow(std::exception_ptr) not working

After JAVA I had to tackle C ++. The simplest example, but it doesn’t work for me:

#include <iostream>

void handle_exceptions(std::exception_ptr e);

   std::exception_ptr e;   

int main()
{


    try {
        throw new std::exception("test");
    }
    catch (...) {
        e = std::current_exception();
        handle_exceptions(e);
    }
} // END: main

void handle_exceptions(std::exception_ptr e)
{

    try {

        if (e) { std::rethrow_exception(e); }

    }
    catch (const std::exception & e) {

        std::cerr << std::endl << e.what();
        std::exit(0);
    }
} // END: handle_exception()

The line of std::rethrow generates an unhandled exception: Возникло необработанное исключение по адресу 0x766A08B2 в ConsoleApplication1.exe: исключение Microsoft C++: std::exception по адресу памяти 0x00BAF1F0.

Possible std::exception_ptr is not NULL. Wrong memory access, but why?

Upvotes: 0

Views: 282

Answers (1)

rjhcnf
rjhcnf

Reputation: 1057

Change

throw new std::exception("test");

to

throw std::exception("test");

By the way, std::exception("test") with "test" passed as an argument doesnt compile in my case with g++.

Upvotes: 2

Related Questions