6

What is the defined behavior of the following program, if any?

#include <iostream>
#include <exception>
#include <cstdlib>

void i_throw()
{
    std::cout << "i_throw()" << std::endl;
    // std::terminate() is noexcept so if the terminate handler throws...
    // then the terminate handler is called...
    // std::terminate is [[noreturn]] so don't return
    try
    {
        throw 7;
    }
    catch(...)
    {
        std::cout << "caught exception, re-throw()-ing" << std::endl;
        throw;
    }
    std::cout << "got here!" << std::endl;
    std::abort();
}

int main()
{
    std::set_terminate(i_throw);
    throw;
    std::terminate();
}

With gcc and clang I get the following output:

i_throw()
caught exception, re-throw()-ing
Aborted (core dumped)

Example edited after first few comments.

(I don't know why I have both throw; and std::terminate();. I don't want to change the example so just pretend only one of those two is there.)

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • 1
    This program causes infinite recursion. Like any other infinite recursion, it will eventually end abnormally due to exhausting the implementation's resource limits (**1.4/(2.1)**). Note that no exceptions are being thrown here, and `terminate`'s `noexcept` specification is irrelevant. Executing `throw;` with no operand while no exception is being handled calls `terminate()` (**5.17/4**) – Igor Tandetnik Feb 27 '15 at 03:58
  • @IgorTandetnik I think I see an answer in your comment. Could you clarify what you mean by no exceptions are being thrown? – Praxeolitic Feb 27 '15 at 04:02
  • The terminate handler is required to terminate execution of the program. Yours doesn't. – T.C. Feb 27 '15 at 04:04
  • @T.C. It was going to though. Does that mean a valid terminate handler can't allow exceptions to bubble up? – Praxeolitic Feb 27 '15 at 04:10
  • @Praxeolitic 1) There's a comment in the code suggesting the OP believes there's an exception being thrown, and a call to `terminate()` is caused by that exception propagating up and out of `terminate` and the latter being `noexcept`. But `throw;` without argument only throws an exception if one is being handled at the moment; otherwise, it just calls `terminate` directly. This latter case in fact occurs in the example. – Igor Tandetnik Feb 27 '15 at 04:18
  • @Praxeolitic 2) No, I don't believe `terminate_handler` is allowed to throw an exception. **18.8.3.1/2** A `terminate_handler` shall terminate execution of the program without returning to the caller. – Igor Tandetnik Feb 27 '15 at 04:20
  • @IgorTandetnik Thanks for pointing that out. I didn't realize exactly what a `throw;` by itself was doing. Example edited. – Praxeolitic Feb 27 '15 at 04:28
  • With the edit, I believe your program exhibits undefined behavior, as `i_throw` does not satisfy the requirements on `terminate_handler`. – Igor Tandetnik Feb 27 '15 at 04:30

1 Answers1

2

The above question can be boiled down to understanding the behavior of the following two code snippets.

Sample 1: throw with no active exception

int main()
{
    try{
        throw;
    }catch(...){
        std::cout<<"caught"<<endl;  //we never reach here
    }
    return 0;
}

If you run the above code it crashes as below

terminate called without an active exception
Aborted (core dumped)

Sample 2: throw with active exception

int main()
{
    try{
        throw 7;
    }catch(...){
        std::cout<<"caught"<<endl;  //will be caught
    }
    return 0;
}

Running it gives a predictable output

caught

If you generate the assembly of the code ( g++ -S option ). You'll notice the following cxx_abi calls for throw vs throw 7

throw; gets converted to call __cxa_rethrow

and

throw 7; gets converted to call __cxa_throw

Here's the code for __cxa_throw

extern "C" void
__cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo,
             void (_GLIBCXX_CDTOR_CALLABI *dest) (void *))
{
  PROBE2 (throw, obj, tinfo);

  __cxa_eh_globals *globals = __cxa_get_globals ();
  globals->uncaughtExceptions += 1;

  // code removed for brevity 
  //.......
  // Below code throws an exception to be caught by caller

  #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
    _Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
  #else
    _Unwind_RaiseException (&header->exc.unwindHeader);
  #endif

  // Some sort of unwinding error.  Note that terminate is a handler.
  __cxa_begin_catch (&header->exc.unwindHeader);
  std::terminate ();
}

So, in the OP Code throw 7; will be caught by corresponding catch(...) and will be re-thrown by throw;

Here's the code for __cxa__rethrow

extern "C" void
__cxxabiv1::__cxa_rethrow ()
{
  __cxa_eh_globals *globals = __cxa_get_globals ();
  __cxa_exception *header = globals->caughtExceptions; // We are not re

  globals->uncaughtExceptions += 1;

  // Watch for luser rethrowing with no active exception.
  if (header)
    {
      // Code removed for brevity
      // .....
      // Below code rethrows the exception
      #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
      _Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
      #else
      #if defined(_LIBUNWIND_STD_ABI)
      _Unwind_RaiseException (&header->unwindHeader);
      #else
      _Unwind_Resume_or_Rethrow (&header->unwindHeader);
      #endif
      #endif
    }
  std::terminate ();
}

In both the cases, we could see that std::terminate() is not yet called from the __cxx_*. After being thrown by above abi's we are at the following location in the code.

refer to the cxx_abi for terminate the code.

void
__cxxabiv1::__terminate (std::terminate_handler handler) throw ()
{
  __try 
    {
      handler ();      // Our handler has thrown an int exception
      std::abort ();
    } 
  __catch(...)  // Exception is caught here and process is aborted.
    { std::abort (); } 
}

void
std::terminate () throw()
{
  __terminate (get_terminate ());
}

Summary

As per my understanding, the re-throwing of the exception from the handler is resulting in catching the re-thrown exception in __cxxabiv1::__terminate. Where it calls abort(). Clearly, the std::terminate() [from __cxa_rethrow] method didn't come into the picture, that's why the control never reached std::cout << "got here!" << std::endl;

Infinite Recursion

What happens if we changed the terminate_handler to the following:

void i_throw()
{
    std::cout << "i_throw()" << std::endl;
    throw;
    std::cout << "got here!" << std::endl;
    std::abort();
}

To understand this, we could look at __cxa_rethrow() as mentioned above.

Since, there's no active exception that is being thrown, __cxa_rethrow() would end-up calling std::terminate(), thereby, causing infinite recursion.

sanjayk79
  • 542
  • 3
  • 15