- 浏览: 389570 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
As in my prevoius example in java - common practice to rethrow exceptions, we have seen that it is always good to rethrow an new exception with the old exception as its inner exception, and it is not too bad after to just throw the exception that you have just catched (only you loose the information that the original exception has).
in C++, there is a special syntax to rethrow the exception that has been caught. which is just call "throw" without any argument, here is what you do
try { // statements. } catch (const exception& ex_) { throw; }
because of the "const reference &" syntax in the code above.
Since not all C++ classes has the necessary call stack support, we might not be able to show every clearly the major diference in terms of the call stack.
class UnsupportedException : public exception { public: const exception * const innerException() { return _innerException; } UnsupportedException(const char* message_, const exception * const innerException_) : exception(message_), message(message_), _innerException(innerException_) { } UnsupportedException(const UnsupportedException& rhs) : exception(rhs.message), message(rhs.message), _innerException(rhs._innerException) { } /** one thing is missing which is called delegaing constructor */ virtual const char * what() { return ((const UnsupportedException *) this)->what(); } virtual const char * what() const { char* b = new char[100]; strcpy(b, message); if (_innerException != NULL) { strcat(b, "cause by \n"); strcat(b, _innerException->what()); } return b; } private: const char * message; const exception * const _innerException; }; void generateException() { const UnsupportedException exc = UnsupportedException("not implemented 'generateException'", new exception("Inner exception")); throw exc; } void rethrowException() { try { generateException(); } catch (exception& ex_) { throw; } } void rethrowDestroyOriginalInfo() { try { generateException(); } catch (const exception & ex_) { throw ex_; } } void rethrowExceptionWithInnerException() { try { generateException(); } catch (exception& ex_) { throw UnsupportedException("wrapped new UnsupportedException", new UnsupportedException(dynamic_cast<const UnsupportedException &>(ex_))); } } int _tmain(int argc, _TCHAR* argv[]) { try { rethrowException(); } catch (const UnsupportedException & ex_) { cout << "in catch (const exception & ex_)" << endl; const char * what = ex_.what(); cout << what << endl; } cout << "============================ " << endl; try { rethrowDestroyOriginalInfo(); } catch (const UnsupportedException & ex_) { cout << "in catch (const exception & ex_)" << endl; const char * what = ex_.what(); cout << what << endl; } catch (const exception & ex_) { cout << "in const exception & ex_" << endl; const char * what = ex_.what(); cout << what << endl; } cout << "============================ " << endl; try { rethrowExceptionWithInnerException(); } catch (const UnsupportedException & ex_) { cout << "in catch (const exception & ex_)" << endl; const char * what = ex_.what(); cout << what << endl; } catch (const exception & ex_) { cout << "in const exception & ex_" << endl; const char * what = ex_.what(); cout << what << endl; } return 0; }
and the output of the running the above program is like this:
in catch (const exception & ex_) not implemented 'generateException'cause by Inner exception ============================ in const exception & ex_ not implemented 'generateException' ============================ in catch (const exception & ex_) wrapped new UnsupportedExceptioncause by not implemented 'generateException'cause by Inner exception
As you can see, with the "throw;" the standard way of rethrowing excpetion, it has the correct error messages and with the "throw ex;", the real type of exception may be lost in the catch handler (you can see the second output", the third is what we try to replicate for the c# or java style of throwing exception - which to be honest, I am not sure if that is indeed what we really what we wantted to do in C++;
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15285首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 965RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ -typeid operator
2012-10-15 22:30 1032typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 738There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 784we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 914As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 723There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 931Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 926As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 951As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1287in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 905class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 959template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1183There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 806the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 554The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1135In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 838A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1129You can overload the subscript ...
相关推荐
测试者一掷千金 描述 不同测试人员: 将异常抛出另一个异常。 将异常抛出到catch中-相同的异常。 抛出异常到catch中-两种不同的异常。 将日志错误扔到catch中。... 将日志错误扔到catch中。... :)
// A C++ Program without class and object! #include using namespace std; const int N=200; void strUpper(char *s); void strLower(char *s); int main(){ char ms[N]; cout; cin.getline(ms,N); ...
错误处理基于异常的错误处理依赖于std :: current_exception和std :: rethrow_exception。 如果您预计会遇到许多错误情况,这些速度可能会很慢。其他实施std :: future 理想情况下,我们可以将核心库功能用于期货...
The entire Java language, including Java 7-specific features such as switch on string, try-with-resources, final rethrow, multicatch, and SafeVarargs A huge assortment of Java 7 APIs, beginning with ...
var rethrow = require ( 'rethrow' ) ; 有关用法示例,请参见。 原料药 在上下文中将给定的err重新抛出给给定lineno filename中有问题的模板表达式。 参量 err {Error} :错误对象 filename {String} :模板的...
linux下使用nodejs 连接数据库mysql
var rethrow = require ( 'template-error' ) ; rethrow ( '\n<%= a %>\n<%= b %>' , { data : { a : 'aaa' } } ) ; 当未定义模板变量时(例如上例中的<%= b %> ,而不是得到一条神秘的错误...
% 索引错误% 抓住% % 可能在这里做一些家务% rethrow(lasterror); % 结尾% % >> 测试错误% ??? 试图访问 a(4711); 索引越界... % % >> 分散% ??? 试图访问 a(4711); 索引越界... % Error in ==> testdisperror at...
Testcase for proper handling of rethrow.
巴别塔尝试捕获装载机该加载器使用babel转换文件并使用try catch子句包装所有函数(包括类方法和箭头函数),它收集特定信息:文件名,函数名,行号,使用AST... //a temp directory relate to project root dirctory,
rethrow ( String name , Throwable t ) { logger . info( " Hello, {}! " ); logger . warn( " Now I will wrap and throw {} " , t); throw new RuntimeException (t); } } 提供错误模式 目前该产品提供9种花样。 ...