c++ - exception specification follow up discussion

In previous - respot - c++ - A programmatic look at the exception specifications, I discusshe exception specification, which as described as the shadow type exception;

 

 

here, I am going to show some code examples on how to declare the exception specification. , and what is the moral of using the exception specification.

 

 

we have seen the iStack declaration before at this post:  c++ function try block; now is that the code with the exception specification.

 

 

 

 

/**
* @ verion 2
* this version has the exception spcification on it, where it has in it declaration  the exception that it has 
* 
*  
*/
class iStack 
{
public : 
	iStack(int capacity ) : _stack(capacity) , _top(0) { }
	void pop(int & top_value) throw (popOnEmpty);
	void push (int value) throw (pushOnFull);

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

	int size();

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

 

the implementation remains the same as before, only the difference now is that the new implemenation now has the exception specification.

 

 

/**
* @ version 2
* this version contains the function declaration contains the function exception specification
*/
void iStack::pop(int &top_value) throw (popOnEmpty)
{
 // same as before
}


void iStack::push(int value) throw (pushOnFull)
{
// same as before

}
 

 

and below is some more code to potray the "shadowed type system"

 

 

/**
* exception declaration is part of the function interface.
*/
typedef int exceptionType;
void no_problem() throw ()
{}
void doit(int, int) throw (std::string,  exceptionType)
{}
void recoup(int , int ) throw (exceptionType) {}

int exception_specification() 
{
	// ok, recoup is as restrictive as pf1
  void (*pf1)(int, int) throw (exceptionType) = &recoup;
  // OK, no_problem is more restrictive than pf2
  void (*pf2)() throw (std::string) = &no_problem;
  // it is error as according to the book, however, it does work 
  // on VC++, it does not throw out errors?
  void (*pf3)(int , int) throw (std::string) = &doit;
  return 0;
}
 

 

the most notable point is that the supposed to be error code compiles with both the VC++ and the gnu c++ 4.6.1 compiler.

 

 

why is that?

 

 

I think there is a trend in the C++ community where there people are relying less and less on the excpetion type system because it is unmatured system (unlike java, which is a matured one); and I want to emphasize on the two morals about how to use the excepiton specification as below.

 

 

Moral #1: Never write an exception specification.

Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.

 

 

 

and the reason behind is :

 

bullet

Guarantee Enforce at runtime that functions will only throw listed exceptions (possibly none).

bullet

Enable or prevent compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown having to check whether listed exceptions are indeed being thrown.

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值