C++: Exception: Out of order execution?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 2
Reputation: erizy is an unknown quantity at this point 
Solved Threads: 0
erizy erizy is offline Offline
Newbie Poster

C++: Exception: Out of order execution?

 
0
  #1
Oct 18th, 2007
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!!!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #2
Oct 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: erizy is an unknown quantity at this point 
Solved Threads: 0
erizy erizy is offline Offline
Newbie Poster

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

 
0
  #3
Oct 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #4
Oct 18th, 2007
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:
  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.)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,855
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

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

 
0
  #5
Oct 18th, 2007
Not in C++, might be possible for a language which takes care of multiple processors
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #6
Oct 18th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC