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;
}
posted @ 2011-10-25 13:27  韩冬冬  阅读(200)  评论(0编辑  收藏  举报