ITeye博客

c++ 异常处理

韩冬冬 · 2010-10-23 10:09 · c++ 语言基础 · 编程语言 · C · C++ · C# · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。