c++ - exception specification follow up discussion_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > c++ - exception specification follow up discussion

c++ - exception specification follow up discussion

 2012/9/8 11:51:01  joe.bq.wang  程序员俱乐部  我要评论(0)
  • 摘要:Inprevious-respot-c++-Aprogrammaticlookattheexceptionspecifications,Idiscussheexceptionspecification,whichasdescribedastheshadowtypeexception;here,Iamgoingtoshowsomecodeexamplesonhowtodeclaretheexceptionspecification.
  • 标签:c++

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.

?

?

?

?

?

发表评论
用户名: 匿名