//throw yichang();//此时必须得加上括号
//作用:(均在catch{}里体现)
// 1.报告错误
// 2.修正错误
#include <iostream>
#include <string>
using namespace std;
class yichang {
string s;
public:
yichang(string str="自定义异常\n"):s(str){}
void what(){
std::cout<<s;
}
};
void function()
{
throw yichang();//此时必须得加上括号
}
int main() {
try{
if(true)
throw yichang("抛出错误\n");//刚开始这里就错误了
}catch(yichang &a){//catch(yichang a)//引用
a.what();
}
int a;
while(1)
{
try{
cin>>a;
if(1==a)
function();//执行这句则跳过下面的break
break;
}catch(yichang a){///非引用
a.what();
}
}
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交