| | |
C++: Exception: Out of order execution?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
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!!!
Please search "QUESTION" and see my question.
Thank you for helping!!!
cpp Syntax (Toggle Plain Text)
#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.
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.
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:
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.)
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)
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
![]() |
Similar Threads
- 'Object variable or With block variable not set' Error (ASP.NET)
- Help with a cast error (ASP.NET)
- I need help with a Security Exception (ASP.NET)
- help me(cpu scheduling) (Computer Science)
- Login used to work (ASP.NET)
- help connecting a form to a database.. error message (ASP.NET)
- ADO.NET Specified cast is not valid (ASP.NET)
- loads htm but not aspx (ASP.NET)
- Create Windows Authentication (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: How to play mp3 and sfx sound files in windows mobile 5.0 ?
- Next Thread: how to append input at the end of the old data in a text file?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






