這就是我想要做的(這是一個真正的項目的簡化):如何正確實施最終條件?
int param;
int result;
void isolated(int p) {
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
} catch (...) {
throw "problems..";
}
}
我不能改變的方式process()
作品,因爲在這個項目不會創建這個功能,是第三方功能。它適用於全局變量param
和result
,我們不能改變這一點。
當isolated()
從另一個參數process()
回叫時出現問題。我想知道這種情況,但不知道該怎麼做,因爲finally
在C++中不存在。我覺得我應該使用RAII技術,但在這種情況下無法正確理解如何去做。
這就是我如何與代碼重複使它:
int param;
int result;
void isolated(int p) {
static bool running;
if (running) {
throw "you can't call isolated() from itself!";
}
running = true;
param = p;
try {
// make calculations with "param" and place the
// result into "result"
process();
running = false;
} catch (...) {
running = false; // duplication!
throw "problems..";
}
}
我不明白。您想做什麼? (假設'finally'存在) – kennytm 2010-07-16 08:33:06
問題是,我應該在* catch和'try'中放置*標誌,這顯然是代碼重複。我現在將延長這個問題 – yegor256 2010-07-16 08:38:53
這是不清楚什麼是拋出異常。 – aggsol 2010-07-16 08:46:01