Lily
Lily

Reputation: 846

C++ program terminates without executing the catch block

I'm running a code in parallel using mpi my program terminates and displays Assertion (unsigned long)(size) >= (unsigned long)(nb) failed terminated by signal 6

I added to my main try and catch as the following

   int main(int argc, char *argv[])
 {
    MPI_Init(&argc, &argv);
    int rankid;
    MPI_Comm_rank(MPI_COMM_WORLD, &rankid);

    try
    {
      call functions 
     }
    catch(...)
     {
        cout<<"error from "<<rankid<<endl;
        throw;
      }
     return 0;
 }

I'm compiling my code using mpicxx example.C and running it by

 mpirun -np 2 ./a.out

my code terminates without printing the phrase in the catch, is it try and catch supposed to force my program to execute what is in the catch

Upvotes: 0

Views: 659

Answers (1)

Peter
Peter

Reputation: 36617

Somewhere, among your call functions, there is probably a usage of assert(). If the assertion fails, the result is exiting the program by calling abort(), which (with your implementation i.e. compiler/library) generates a SIGABRT (value 6) signal.

The thing is, abort() is not required to - and generally does not - throw a C++ exception (since it was inherited from C, a language which knows nothing about C++ exceptions). Similarly, raising a SIGABRT signal (the means your implementation uses in abort()) exits the program in a manner that has nothing to do with C++ exceptions.

The only things a catch(...) clause will catch are C++ exceptions. It is not a catch-all (no pun intended) for intercepting any event that causes program termination.

You can possibly intercept the SIGABRT signal by using the signal() function to establish an appropriate signal handler. In practice, it might be more fruitful to work out what is causing your call function to call abort(). In other words, ensure the condition it is asserting is valid. To work out how to to that, you will need to read documentation for the functions being called.

Upvotes: 2

Related Questions