本例外處理系列為【實驗性質】,研究進行中......。
isItError 來表示是否發生錯誤。isItError 的原因,不再需要回傳值必須作錯誤碼處理的約定。
throw 語意與真實的語意不同,改名為 check。try 和 throw(新的check) 的使用方式
isItError 的狀態、數值。throw 來拋出錯誤(設 isItError 為一、退出函數)。back 巨集的參數 back_ex 改名為 back_point 。isItError 來表示是否發生錯誤。int isItError = 0;
throw 來拋出錯誤(設 isItError 為一、退出函數)。#define throw(...) \
\
isItError = 1; \
return __VA_ARGS__;
try 和 throw(新的check) 的使用方式
isItError 的狀態、數值。#define try(ex_name, back_point) \
\
if(isItError){ \
error_place = #back_point; \
goto ex_name;\
} \
back_point: \
#define check(...) \
\
if(isItError){ \
return __VA_ARGS__; \
}
catch 的實現#define catch(ex_name) \
\
if(!isItError) return 0; \
ex_name:
back 的實現#define back(back_point) \
\
if(!strcmp(error_place, #back_point)){ \
isItError = 0; \
goto back_point; \
}
void can_not_be_negative(double input) {
if (input < 0) {
throw();
}
}
double my_sqrt(double input) {
can_not_be_negative(input); check(0.0);
return sqrt(input);
}
int main() {
my_sqrt(-10); try(ex_can_not_be_negative, ex1);
my_sqrt(20); try(ex_can_not_be_negative, ex2);
my_sqrt(-30); try(anthor_ex, ex3);
/*... any things ...*/
system("pause");
catch (ex_can_not_be_negative) {
printf("[ex_can_not_be_negative] exception is happened!\n");
printf("Input can't no be negative.\n");
}
back(ex1); back(ex2);
catch (anthor_ex) {
printf("[anthor_ex] exception is happened!\n");
printf("Input can't no be negative.\n");
}
back(ex3);
}
[ex_can_not_be_negative] exception is happened!
Input can't no be negative.
[anthor_ex] exception is happened!
Input can't no be negative.
Press any key to continue . . .
try 需要設置返回點,麻煩。catch 結束後須加上 back , back 需要對應、適合的返回點,
back ,使用方式繁複。goto 是短程跳躍,無法直接回到最適當的位置。
主要問題是無法自動設置及判斷返回點,導致使用上的麻煩。