我正在尋找一種在出現錯誤時在if和else塊之間切換的方法。例如:在if/else塊之間進行切換錯誤
cout << "Enter 1 or 2 as your choice...";
cin >> choice;
if(choice==1) {
//do something here
//if error occurs....
} else if(choice==2) {
//switch to this else block
}
我玩過try/throw/catch但看起來catch必須遵循try語句。任何想法/建議,將不勝感激!
您可以將'else if'部分的主體放在一個函數中,然後從兩個地方調用它。 – Praetorian 2012-03-08 23:08:26