944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1432
  • C++ RSS
Oct 18th, 2007
0

C++: Exception: Out of order execution?

Expand Post »
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!!!

cpp Syntax (Toggle Plain Text)
  1.  
  2. #include <cstdio>
  3. using namespace std;
  4. class E1 {
  5. public: E1() {
  6. int i = 1;
  7. int j = 2;
  8. int k = 3;
  9. printf("i = %d, j = %d, k = %d\n", i, j, k);
  10. // printf("E1(): throw()\n");
  11. // throw 1;
  12. }
  13. public: ~E1() {
  14. printf("~E1()\n");
  15. }
  16. };
  17.  
  18. class E2 {
  19. public: E2() {
  20. int i = 1;
  21. int j = 2;
  22. int k = 3;
  23. printf("i = %d, j = %d, k = %d\n", i, j, k);
  24. printf("E2(): throw()\n");
  25. throw 1;
  26. }
  27. public: ~E2() {
  28. printf("~E2()\n");
  29. }
  30. };
  31.  
  32. class D {
  33. private: E1* e1;
  34. private: E2* e2;
  35. private: int marker;
  36. public: D() : marker(0), e1(0), e2(0){
  37. try {
  38. printf("D()\n");
  39. e1 = new E1();
  40. marker = 1;
  41. e2 = new E2();
  42.  
  43. // QUESTION:
  44. // Is it possible that marker = 2 be executed before E2()
  45. // throws in modern out-of-order processors?
  46.  
  47. marker = 2;
  48. } catch(...) {
  49. printf("marker = %d\n", marker);
  50. }
  51.  
  52. if (marker == 0) {
  53. }
  54. else if (marker == 1) {
  55. delete e1;
  56. } else if (marker == 2) {
  57. delete e1;
  58. delete e2;
  59. }
  60. }
  61.  
  62. public: ~D() {
  63. printf("~D()\n");
  64. }
  65. };
  66.  
  67. int main() {
  68. D d;
  69. return 0;
  70. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
erizy is offline Offline
2 posts
since Oct 2007
Oct 18th, 2007
0

Re: C++: Exception: Out of order execution?

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 18th, 2007
0

Re: C++: Exception: Out of order execution?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
erizy is offline Offline
2 posts
since Oct 2007
Oct 18th, 2007
0

Re: C++: Exception: Out of order execution?

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:
C++ Syntax (Toggle Plain Text)
  1. bool ok = true;
  2. e1 = new E1();
  3. marker = 1;
  4. try { e2 = new E2(); } catch (...) { ok = false; }
  5. marker = 2;
  6. ...
  7. 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.)
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 18th, 2007
0

Re: C++: Exception: Out of order execution?

Not in C++, might be possible for a language which takes care of multiple processors
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 18th, 2007
0

Re: C++: Exception: Out of order execution?

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC