ILE C/C++ Programmer's Guide


Avoiding Uncaught Exceptions by Scoping to a Single Activation Group

C++ language onlyIn ILE, the effect of the set_terminate() function is scoped to an activation group. The following figure provides an example of how this can affect the compilation of code that is ported from a non-ILE platform.

Figure 232. Example of Code Ported to ILE that Results in an Uncaught Exception


// File main.c
 
  #include <stdio.h>
  #include <stdlib.h>
  #include <iostream.h>
  #include <terminat.h>
 
   void a();
   void my_terminate();
 
   int main() {(2)
      set_terminate(my_terminate);
      try {
          a();
      }
      catch(...) cout << "failed" << endl;
   }
 
// File term.c(1)
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <iostream.h>
 void my_terminate() {
    cout << "failed" << endl;
 }
 void a() { throw 7; }(3)

In Figure 232:

  1. Both my_terminate() and a() reside in a service program, which runs, by default, in a single activation group (for example, "B").
  2. The main() procedure runs, by default, in another activation group (for example, "A").
  3. In this scenario, the exception thrown by a() cannot percolate up to main() because the activation group in which set_terminate is executed is terminated before my_terminate() can be invoked, unless the default activation treatment is overridden.

As a result, a CEE9901 error message is sent to main().

When you port code from another platform to ILE, you need to ensure that the following functions run in the same activation group:


[ Top of Page | Previous Page | Next Page | Table of Contents ]