---
title: "c++ 异常处理"
author: "韩冬冬"
date: "2010-10-23T10:09:44Z"
tags: ["C", "C#", "C++"]
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2010-10-23T10:20:24Z"
id: 791990
url: "https://handongdong.iteye.com/blog/791990"
---

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