1、在当前函数中简单直接使用
for (int i = 0; i < 5; i++){
std::vector<int> temp;
if (i != 3){
temp.push_back(i);
}
try{
if (temp.size() == 0){
throw "temp is empty!\n";
}
}
catch (char* e){
printf(e);
continue;
}
std::cout << temp[0] << std::endl;
}
2、在某一个函数中处理调用的子函数的异常
int add(std::vector<int> &tmp, int a){
if (tmp.size() == 0)
throw "tmp is empty!\n";
return tmp[0] + a;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; i++){
std::vector<int> temp;
int b = 0;
if (i != 3){
temp.push_back(i);
}
try{
b=add(temp, i);
}
catch (char* e){
printf(e);
continue;
}
std::cout << temp[0]<<" "<<i<<" "<<b << std::endl;
}
return 0;
}