c++ function try block

本文详细介绍了如何在C++中使用异常处理来改进函数实现,以栈操作为例展示了如何优雅地处理空栈弹出和满栈插入情况,通过引入自定义异常类和尝试块来实现错误捕获和处理,提高了代码的健壮性和易读性。

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

function try block is a block which the entire body of the function body is encosed in a try cluase block;

 

for better illustration, let see some examples which can better serve for our dicussion purpose later on the exception handling issues. 

 

Let's first introduce the the test code that we are going to use, it is a home made iStack class; 

 

the header file of the stackExcp.h is as follow.

 

 

/***
* @summary
*  stackExcep.h is the header file that define the class that is called iStack, which we will use as the data object to manipulate and demonstate the use 
* of exception in C++
*/

#include <vector>
using std::vector;

class iStack 
{
public : 
	iStack(int capacity ) : _stack(capacity) , _top(0) { }
	void pop(int & top_value) ;
	void push (int value);

	bool full();
	bool empty();
	void display();

	int size();

private: 
	int _top;
	vector <int> _stack;
};


/**
* @Summary
* to demonstrate the use of Exception, we are going to define two classes, one is the 
*    popOnEmpty()
* the other is 
*    pushOnFull
*/
class popOnEmpty { /* ....  */ };

class pushOnFull { /* ... */ };

 

 

part of the stackExcp.'s implementation is as follow.

 

 

#include "stackExcp.h"
#include <iostream>
using std::cout;
using std::endl;

void iStack::pop(int &top_value)
{
	if (empty()) 
	{
		throw popOnEmpty();
	}
	top_value = _stack[--_top];
	cout << "istack::pop(): " << top_value << endl;
}


void iStack::push(int value)
{
	cout << "istack::push( " << value << " ) \n";
	if (full()) 
	{
		throw pushOnFull();
	}
	_stack[_top++] = value;

}

bool iStack::empty()
{
	return _top <= 0;
}

bool iStack::full()
{
	return _top > _stack.capacity(); // a common pitfall is tha the use _stack.size();
}

void iStack::display()
{
	cout << "display: ( ";
	for (int i = 0; i < _top; ++i)
	{
		// a common case of error is that if you write as follow
		// the code will be like this:
		//   cout << _stack[i} << ( i == _top - 1) ? ")" : "";
		// it will output some wierd output , guess the compiler try to output the value of the condition test part of the  the ternary expression
		// that is because of the order of the execution , 
		// where the ?: has a lower precedence than the << operator
		// so it will first output the evalution value of the condition expression and then if depends on the return resutlt, which is a 

		cout << _stack[i] << " " << ((i == _top - 1) ? " )" : "") ;
		if (i != 0 && i % 8 == 0 || i == _top - 1 ) cout << endl;
	}

}

 

normally we will write c++ code as follow :

 

 

void intermixedTryClause()
{
	iStack stack(32);

	for (int ix = 0;ix < 51; ++ix)
	{
		try 
		{
			if (ix % 3 == 0) {
				stack.push(ix);
			}
		}
		catch (pushOnFull) 
		{
			//cout << "caught error on pushOnFull" << endl;
		}
		if (ix % 4 == 0) 
		{
			stack.display();
		}


		try 
		{
			if (ix % 10 == 0)
			{
				int dummy;
				stack.pop(dummy);
				stack.display();
			}
		}
		catch (popOnEmpty) 
		{
			//cout << "caught error on popOnEmpty" << endl;
		}
	}
}
 

as you can see we intermix the body of the function with that of exception handling, which does not follow the separation rule that implementation code should be separated from its error handling code.

 

 

there is a small technique that you can try to make the function body more fun, which as many reader has figured out, it is the function try block, let's see how it works out.

 

 

 

/**
* Below test the function try body techniques where you can put the try clause with the catch clauses right after the function body without 
* the function opening body '{' or the function closing body '}'
*/
void functionTryBody() try 
{
	 iStack stack(32);
	 for (int i = 0; i < 51; ++i ) {
		 int val;
		 if (i % 3 == 0) stack.push(i);
		 if (i % 4 == 0) stack.display();
		 if (i % 10 == 0) { stack.pop(val); stack.display(); } 
	 }
}
catch (popOnEmpty) { /* */ } 
catch (pushOnFull) { /* */ }
 

so directly follow the function declaration is the try keyword , then is the try body followed by several catch handlers and body;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值