There are situations that exception could be throw within constructor and we usually employ auto_ptr to protect resources. But if we do not have auto_ptr can we use the following technique to handle it?

Please search "QUESTION" and see my question.
Thank you for helping!!!

#include <cstdio>
      using namespace std;
      class E1 {
      public: E1() {
                        int i = 1;
                        int j = 2;
                        int k = 3;
                        printf("i = %d, j = %d, k = %d\n", i, j, k);
                        // printf("E1(): throw()\n");
                        // throw 1;
                  }
      public: ~E1() {
                        printf("~E1()\n");
                  }
      };
       
      class E2 {
      public: E2() {
                        int i = 1;
                        int j = 2;
                        int k = 3;
                        printf("i = %d, j = %d, k = %d\n", i, j, k);
                        printf("E2(): throw()\n");
                        throw 1;
                  }
      public: ~E2() {
                        printf("~E2()\n");
                  }
      };
       
      class D {
      private: E1* e1;
      private: E2* e2;
      private: int marker;
      public: D() : marker(0), e1(0), e2(0){
                        try {
                              printf("D()\n");
                              e1 = new E1();
                              marker = 1;
                              e2 = new E2();

      // QUESTION:
      // Is it possible that marker = 2 be executed before E2()
      // throws in modern out-of-order processors?
       
                              marker = 2;
                        } catch(...) {
                              printf("marker = %d\n", marker);
                        }
       
                         if (marker == 0) {
                        }
                        else if (marker == 1) {
                              delete e1;
                        } else if (marker == 2) {
                              delete e1;
                              delete e2;
                        }
                  }
       
      public: ~D() {
                        printf("~D()\n");
                  }
      };
       
      int main() {
            D d;
            return 0;
      }

In short: no.

If I understand your question properly, what you want to do is execute all the code in the try {...} block before the exception transfers control to the catch (...) {...} block.

If that is the case, you need to reconsider how exceptions work. Anytime an exception is thrown control immediately transfers out of the current block and up the chain until it is handled.

This is also reasonable, because the error occurred when marker had a value of 1, not 2. In your example, you don't want marker to have a value of 2 if the E2 constructor throws an exception, since further on you'll try to delete E2 when it doesn't exist...

However, if you just have a list of initializations that must occur before some exception may, put them before the code that may cause the exception.

Hope this helps.

Thanks for answering my first post !

Is it possible that the instruction of "marker = 2;" was fetched simultaneously and was dispatched and executed before the throw of E2() is executed?

In that case my program will fail since it will delete e2 which was not constructed fully.

No.

Re-read my first answer. Exceptions immediatly transfer control away from the current block. No code following the exception will ever be executed.

If you really want to, you can wrap the offending code in a try block and re-raise the exception (or, as in the following example, a new exception) later:

bool ok = true;
e1 = new E1();
marker = 1;
try { e2 = new E2(); } catch (...) { ok = false; }
marker = 2;
...
if (!ok) throw "E2() failed to construct.";

BTW. If this is production code get rid of the catch (...) stuff and explicitly list all the exceptions that you are prepared to handle. (Some exceptions are so awful that they actually do need to make it back to the system.)

Not in C++, might be possible for a language which takes care of multiple processors

Actually, I think I was thinking of Python or something. Either way, it is still a good rule of thumb, even in C++. There should generally only be a maximum of one place that catches all possible exceptions. Number of processors is immaterial... Alas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.