- 浏览: 403137 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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 :
![]() |
Guarantee Enforce at runtime that functions will only throw listed exceptions (possibly none). |
![]() |
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. |
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15356首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1027RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 781It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 979As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1069typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 792There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 835we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 999As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 762There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 966Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 988As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1005As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1350in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 948class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 997template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1220There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 864the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 597The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1178In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 884A class member operator new() c ...
相关推荐
warning: gcc-c++-4.4.7-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY error: Failed dependencies: gcc = 4.4.7-3.el6 is needed by gcc-c++-4.4.7-3.el6.x86_64 libstdc++ = 4.4.7-...
gcc-c++-3.4.6-3.1.x86_64.rpm
Linux安装Oracle先决条件gcc-c++-3.4.6-8.x86_64.rpm
gcc-c++-4.4.4-13.el6.x86_64.rpm
compat-gcc-c++-7.3-2.96.128.i386.rpm
This Standard specifies requirements for implementations of the C++/CLI binding. The first such requirement is that they implement the binding, and so this Standard also defines C++/CLI. Other ...
安装oracle11g是提示缺少包,安装gcc-c++-4.4.7-4.el6.x86_64.rpm 后,再次检查不再提示。
compat-gcc-c++-7.3-2.96.122.i386.rpm
gcc-c++-4.1.2-54.el5.x86_64.rpm
适合 redhat enterprise linux 5 上安装
gcc-c++-4.4.6-4.el6.x86_64.rpm
Linux下的gcc,RPM格式,共享资源
gcc-c++-4.4.7-4.el6.i686.rpm cpp-4.4.7-4.el6.i686.rpm gcc-4.4.7-4.el6.i686 .rpm libstdc++-devel-4.4.7-4.el6.i686.rpm libstdc++-4.4.7-4.el6.i686.rpm
gcc-c++-4.4.7-3.el6.x86_64.rpm
《Visual C++ 2015-2019 Redistributable:不可或缺的运行环境》 Visual C++ 2015-2019 Redistributable是一款由微软公司推出的库文件,它对于运行依赖于Microsoft Visual C++编译器的软件来说至关重要。这款红分...
LL1语法分析c++实现-first集-follow集-分析表-分析栈.doc
Microsoft Visual C++ (VC++) 是微软开发的一款重要的编程环境,用于创建Windows平台上的应用程序。它提供了C++编译器和其他工具,使得开发者可以利用C++语言进行高效且强大的软件开发。2015到2019年间的版本,包括...
**Microsoft Visual C++ 2015-2022 Redistributable (x64)** 是微软提供的一款关键组件,用于支持运行使用Microsoft Visual C++编译器开发的基于Windows的应用程序。这款软件包包含了运行时库,这些库是许多应用程序...
"Exception C++ & More Exception C++"可能是指两本关于深入理解和应用C++异常处理机制的书籍,其中包含"More Exceptional C++.chm"和"Exceptional C++.chm"这两个章节或部分。 异常处理是C++中的一个核心特性,它...
gcc-c++-3.4.6-3.i386.rpm