异常继承应用

本文深入探讨了异常处理机制,包括如何使用try、catch和throw语句来框定、处理和抛出异常。通过一个具体的例子,展示了如何设计异常类以及如何在实际编程中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常使用三部曲:

1 框定异常(try语句块)

在祖先函数出,框定可能产生错误的语句序列,它是异常的根据。

2 定义异常处理(catch语句块)

将出现异常后的处理过程放在catch块中,以便当异常被抛出,因类型匹配而捕捉是,就处理异常,而不是延时处理,或者不处理。

3 抛送异常(throw语句)

在可能产生错误的函数中定义,如果有错,就抛出异常。

编程前,设计类,同时设计类可能抛出的有关异常类。编程中,当try执行中又异常抛出,则用catch捕捉异常,并当场处理。

异常类设计:

//MyException.h

#include "stdafx.h"

 class MyException
 {
 public:
	 virtual const char *what(){ return "To the Base Exception"; }
 };

 class HardWareError:public MyException
 {
 public:
	 virtual const char *what(){ return "To the HardWare Exception"; }
 };

 class PerformError:public MyException
 {
 public:
	 virtual const char *what(){ return "To the Perform Exception"; }
 };

 class SizeError:public MyException
 {
 public:
	 virtual const char *what(){ return "To the Size Exception"; }
 };

 class HSizeError:public SizeError
 {
 public:
	 virtual const char *what(){ return "To the HSize Exception"; }
 };

 class VSizeError:public SizeError
 {
 public:
	 virtual const char *what(){ return "To the VSize Exception"; }
 };

 class OtherError:public MyException
 {
 public:
	 virtual const char *what(){ return "To the Other Exception"; }
 };


编写抛出并捕捉异常的累:

//exceptionTest.h

#include "stdafx.h"
#include "MyException.h"

class exceptionTest
{
private:
	 void metalPieseProcess(int);
	 void metalGroupProcess(int);
	 void hProcess();
	 void vProcess();
	 void PerformTest();
	 void calcVSize();
	 bool tooSmallHSize(){return true;}
	 bool performTeatFail(){return false;}
	 bool toSmallVSize(){return false;}
public:
	exceptionTest(){}
	~exceptionTest(){}
	void RunTest();
};

//exceptionTest.cpp

#include "stdafx.h"
#include "exceptionTest.h"
#include <exception>
#include <stdexcept>
#include <typeinfo>
using namespace std;

void exceptionTest::RunTest()
{
	try
	{
		for (int i = 0;i < 10; i++)
		{metalGroupProcess(i);
		}
	}
	catch (MyException& e)
	{
		if (e.what() == "To the HardWare Exception")
		{cout<<"停机"<<endl;
		}
		if (e.what() == "To the Perform Exception")
		{cout<<"加工件离机,温柔停机"<<endl;
		} 
		if (e.what() == "To the Other Exception")
		{cout<<"除暴停机"<<endl;
		}
		cout<<"Myexception"<<e.what()<<endl;
	}catch(exception e)
	{
		cout<<"StandardException: "<<typeid(e).name()<<endl;
	}
}

void exceptionTest::calcVSize()
{
	if (toSmallVSize()) throw VSizeError();
}

void exceptionTest::hProcess()
{
	if (tooSmallHSize())
	{throw HSizeError();
	}
}

void exceptionTest::vProcess()
{
	calcVSize();
}

void exceptionTest::PerformTest()
{
	if (performTeatFail())
	{throw PerformError();
	}
}

void exceptionTest::metalPieseProcess(int i)
{
	hProcess();
	PerformTest();
	try
	{
		vProcess();
	}
	catch (VSizeError &e)
	{
		cout<<e.what()<<endl;;
		cout<<"VSizeError Report"<<endl;
	}
}

void exceptionTest::metalGroupProcess(int a)
{
	try
	{
		for (int i = 0; i < 10; i++)
		{
			metalPieseProcess(i);
		}
	}
	catch (HSizeError* e)
	{
		cout<<e->what()<<endl;
		cout<<"Report HSizeError in metalGroupProcess"<<endl;
	}
	catch(PerformError &e)
	{
		cout<<e.what()<<endl;
		cout<<"Report PerformError in metalGroupProcess"<<endl;
		throw;
	}
}

在main函数中,使用调用测试类代码:

exceptionTest *et = new exceptionTest();
et->RunTest();
delete et;
 

使用多态继承方式捕获异常,体现对象编程的优势。                                                                                                                                                                                                                       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值