`
韩冬冬
  • 浏览: 13570 次
  • 性别: Icon_minigender_1
  • 来自: 天津
最近访客 更多访客>>
社区版块
存档分类
最新评论

c++ 异常处理

阅读更多
#include "stdafx.h"
#include <iostream>
using namespace std;

enum EHstate{ noErr, zeroOP,nega, severeError};
enum EHstate state = noErr;
int mathFunc(int i)
{
	if(i == 0)
	{
		throw state;
	}
}
void calculate(int op)
{
	try
	{
		mathFunc(op);
	}
	catch(EHstate &eobj)//声明为引用
	{
		eobj = severeError;//此处未修改全局变量的值
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"全局变量:"<<state<<endl;
	calculate(0);
	cout<<"全局变量:"<<state<<endl;
	return 0;
}

 

#include "stdafx.h"
#include <iostream>
using namespace std;

enum EHstate{ noErr, zeroOP,nega, severeError};
enum EHstate state = noErr;
int mathFunc(int i)
{
	if(i == 0)
	{
		throw state;
	}
}
void calculate(int op)
{
	try
	{
		mathFunc(op);
	}
	catch(EHstate eobj)//此处为对象,不是引用
	{
		eobj = severeError;//修改此局部对象
		throw;//抛出的仍然是原始的异常对象,而非修改的局部对象
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"全局变量:"<<state<<endl;
	try
	{
		calculate(0);
	}
	catch(EHstate eobj)
	{
		cout<<"捕捉的异常对象值:"<<eobj<<endl;
	}
	cout<<"全局变量:"<<state<<endl;
	return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics