简体   繁体   English

C ++程序终止而不执行catch块

[英]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 我正在使用mpi并行运行代码,我的程序终止并显示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 我正在使用mpicxx example.C编译我的代码,并通过运行

 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 我的代码在不打印catch语句的情况下终止,是否是try and catch应该强制我的程序执行catch中的内容

Somewhere, among your call functions , there is probably a usage of assert() . call functions某处,可能使用了assert() If the assertion fails, the result is exiting the program by calling abort() , which (with your implementation ie compiler/library) generates a SIGABRT (value 6 ) signal. 如果断言失败,则通过调用abort()来退出程序,该操作(与您的实现相同,即编译器/库)生成SIGABRT (值6 )信号。

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). 事实是,不需要(通常不需要abort()引发C ++异常(因为它是从C语言继承而来的,C语言对C ++异常一无所知)。 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. 同样,引发SIGABRT信号(意味着您的实现在abort()使用abort()意味着该程序以与C ++异常无关的方式退出程序。

The only things a catch(...) clause will catch are C++ exceptions. catch(...)子句将捕获的唯一内容是C ++异常。 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. 您可以通过使用signal()函数来建立适当的信号处理程序来拦截SIGABRT信号。 In practice, it might be more fruitful to work out what is causing your call function to call abort() . 在实践中,找出导致您的call function调用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. 要解决该问题,您将需要阅读有关所调用函数的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM