异常处理

一.

exception.h

#include <stdexcept>
using namespace std;
class DivideByZeroException:public runtime_error
{
public:
	DivideByZeroException() :runtime_error("attmped to divide by zero"){}
};

exception.cpp

/*异常处理时设计用来处理同步错误的。这些错误发生在一个语句正在执行的时候。
   常见的例子有数组下标越界、运算溢出、除数为0、无效的函数参数、失败的内存分配
   异常处理并不处理相关的异步时间(如磁盘I/O操作、网络信息的到来、鼠标的点击)
   这些与程序的控制流并行与相互独立。
*/
#include "stdafx.h"
#include "exception.h"
#include <iostream>
double divide(int num1, int num2)
{
	if (num2 == 0)
		throw DivideByZeroException();
	return (double)num1 / num2;
}
int main(int argc, char* argv[])
{
	int num1, num2;
	double result;
	cout << "enter two integers:" << endl;
	while (cin >> num1 >> num2)
	{
		try
		{
			result = divide(num1, num2);
			cout << "the result is: " << result << endl;
		}
		catch (DivideByZeroException& divideByZeroException)
		{
			cout << "exception occured: " << divideByZeroException.what() << endl;
			cout << "enter two integers:" << endl;
		}
	}
	return 0;
}

二.

//重新抛出异常
/*异常处理器在接收到异常时,有可能判断出该异常是它不能处理或是只能部分处理的
   在这种情况下,异常处理器将异常处理推给另外一个异常处理器。
   通过语句throw; 重新抛出异常
*/
#include "stdafx.h"
#include <iostream>
#include <exception>
using namespace std;
void throw_exception()
{
	try
	{
		cout << "functon throw_exception throws an exception" << endl;
		throw exception();
	}
	catch (exception&)
	{
		cout << "exception handled in function throw_exception" 
			<<"\nfuncton throw_exception rethrows function"<< endl;
		throw;
	}
}
int main(int argc, char** argv)
{
	try
	{
		cout << "main invokes function throwexception" << endl;
		throw_exception();
		cout << "this should not print" << endl;
	}
	catch (exception&)
	{
		cout << "exception handled in main" << endl;
	}
	cout << "program control continues after in main" << endl;
	return 0;
}

三 .

//堆栈展开
/*当异常被抛出但没有在一个特定的区域内被捕获时,该函数调用堆栈就会展开,
   并试图在下一个外部try...catch快内捕捉该异常*/
#include "stdafx.h"
#include <iostream>
#include <stdexcept>
using namespace std;
void function3()
{
	cout << "in function 3" << endl;
	throw runtime_error("runtime_error in function 3");
}
void function2()
{
	cout << "function 3 is called insied function 2" << endl;
	function3();
}
void function1()
{
	cout << "function 2 is called insied function 1" << endl;
	function2();
}
int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		cout << "function 1 is called insied main" << endl;
		function1();
	}
	catch (runtime_error& error)
	{
		cout << "exception occured: " << error.what() << endl;
		cout << "exception handled in main" << endl;
	}
	return 0;
}

四.

// bad_alloc.cpp : 定义控制台应用程序的入口点。
//

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

int _tmain(int argc, _TCHAR* argv[])
{
	double* ptr[50];
	try
	{
		for (int i = 0; i < 50; i++)
		{
			ptr[i] = new double[50000000];
			cout << "ptr["<<i<<"] points to 50000000 new doubles" << endl;
		}
	}
	catch (bad_alloc& memoryAllocationException)
	{
		cout << "exception occured: " << memoryAllocationException.what() << endl;
	}
	return 0;
}


// set_new_handler.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <new>   //set_new_handler
#include <cstdlib>  //abort
using namespace std;
void custom_newhandler()
{
	cout << "custom_newhandler is called" << endl;
	abort();
}
int _tmain(int argc, _TCHAR* argv[])
{
	double* ptr[50];
	for (int i = 0; i < 50; i++)
	{
		ptr[i] = new double[50000000];
		cout << "ptr["<<i<<"] points to 50000000 new doubles"<< endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值