码迷,mamicode.com
首页 > 其他好文 > 详细

自定义异常

时间:2015-01-23 21:38:19      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:c++

//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;
} 

自定义异常

标签:c++

原文地址:http://blog.csdn.net/h1023417614/article/details/43062049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!