- 浏览: 399556 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 771It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 960As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A 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
compat-gcc-c++-7.3-2.96.128.i386.rpm
gcc-c++-4.4.4-13.el6.x86_64.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 后,再次检查不再提示。
gcc-c++-4.1.2-54.el5.x86_64.rpm
Linux下的gcc,RPM格式,共享资源
最全的 C-C++-(9个中文手册).chm。 C++API中文手册,C++中文函数手册,C++chm中文版手册,C++文档,C++参考手册,C++手册总共包含8个chm文件,C++ 库函数大全手册完整版,而且是chm格式带索引、搜索,用着非常方便...
gcc-c++-4.4.7-3.el6.x86_64.rpm
Microsoft Visual C++ (VC++) 是微软开发的一款重要的编程环境,用于创建Windows平台上的应用程序。它提供了C++编译器和其他工具,使得开发者可以利用C++语言进行高效且强大的软件开发。2015到2019年间的版本,包括...
《Visual C++ 2015-2019 Redistributable:不可或缺的运行环境》 Visual C++ 2015-2019 Redistributable是一款由微软公司推出的库文件,它对于运行依赖于Microsoft Visual C++编译器的软件来说至关重要。这款红分...
**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
对于“gcc&gcc-c++-4.8version”这个压缩包,用户可以期待找到与GCC 4.8相关的RPM包,用于在系统上安装这个特定版本的C和C++编译器。 要安装这些RPM包,首先需要确保系统中已经安装了RPM工具。然后,可以通过以下...
从mini安装版centos7.5提取出来的gcc和gcc-c++安装包,下载后放到服务器解压,并cd到该目录,执行 rpm -Uvh *.rpm,安装完成后,运行gcc -v,g++ -v,会出现版本信息,则完成
非常罕见的gcc-c++ rpm安装包,不用编译
Game Physics CookbookA Discover over 100 easy-to-follow recipes to help you implement efficient game physics and collision detection in your games