Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the function unexpected is not called?

I expect the following code to invoke my unexpected handler, but my terminate handler is called instead:

#include <except>
#include <iostream>

void my_terminate() {
    std::cerr << "my terminate handler";
    std::exit(0);
}

void my_unexpected() {
    std::cerr << "my unexpected handler";
    std::exit(EXIT_FAILURE);
}

#pragma argsused
int main(int argc, char* argv[])
{
    std::set_terminate(my_terminate);
    std::set_unexpected(my_unexpected);
    try {
        throw std::exception();
    } catch (const std::logic_error&) {
    }
    return 0;
}

The C++ Builder 6 Developer's Guide explicitly encourages installing custom unexpected handlers via set_unexpected(). What am I doing wrong, or is this simply a bug in C++-Builder 6?

like image 290
Wolf Avatar asked Aug 20 '14 11:08

Wolf


1 Answers

The handler set by the call to std::set_unexpected (for std::unexpected) will be called when an unexpected exception is thrown; not when an exception is unhandled. The unexpected handler is called when a dynamic exception specification is violated.

By way of example;

void my_terminate() {
    std::cerr << "my terminate handler";
    std::exit(0);
}

void my_unexpected() {
    std::cerr << "my unexpected handler";
    std::exit(EXIT_FAILURE);
}

void function() throw() // no exception in this example, but it could be another spec
{
    throw std::exception();
}

int main(int argc, char* argv[])
{
    std::set_terminate(my_terminate);
    std::set_unexpected(my_unexpected);
    try {
        function();
    } catch (const std::logic_error&) {
    }
    return 0;
}

The output is

my unexpected handler

The handler set by std::set_terminate is called by std::terminate (for numerous reasons as listed in the reference). Of interest here is that the default behaviour when an exception is thrown but not caught is to call std::terminate.

like image 180
Niall Avatar answered Sep 27 '22 19:09

Niall