自定义异常

来源:互联网 发布:淘宝网网店装修教程 编辑:程序博客网 时间:2024/05/24 06:27
//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;} 

0 0
原创粉丝点击