try{} catch(…){}

以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的。例如下面的代码:

  1. try
  2. {
  3. BYTE* pch ;
  4. pch = ( BYTE* )00001234 ;   //给予一个非法地址
  5.  
  6. *pch = 6 ; //对非法地址赋值,会造成Access Violation 异常
  7. }
  8. catch(...)
  9. {
  10. AfxMessageBox( "catched" ) ;
  11. }

这段代码在debug下没有问题,异常会被捕获,会弹出”catched”的消息框。 但在Release方式下如果选择了编译器代码优化选项,则VC编译器会去搜索try块中的代码, 如果没有找到throw代码, 他就会认为try catch结构是多余的, 给优化掉。 这样造成在Release模式下,上述代码中的异常不能被捕获,从而迫使程序弹出错误提示框退出。

那么能否在release代码优化状态下捕获这个异常呢, 答案是有的。 就是__try, __except结构, 上述代码如果改成如下代码异常即可捕获。

  1. __try
  2. {
  3. BYTE* pch ;
  4. pch = ( BYTE* )00001234 ;   //给予一个非法地址
  5.  
  6. *pch = 6 ; //对非法地址赋值,会造成Access Violation 异常
  7. }
  8. __except( EXCEPTION_EXECUTE_HANDLER )
  9. {
  10. AfxMessageBox( "catched" ) ;
  11. }

但是用__try, __except块还有问题, 就是这个不是C++标准, 而是Windows平台特有的扩展。 而且如果在使用过程中涉及局部对象析构函数的调用,则会出现C2712 的编译错误。 那么还有没有别的办法呢?

当然有, 就是仍然使用C++标准的try{}catch(..){}, 但在编译命令行中加入 /EHa 的参数。这样VC编译器不会把try catch模块给优化掉了。

找到一篇比较好的英文文章谈这个问题: http://members.cox.net/doug_web/eh.htm

用C++10 年多了 , 居然这么基础的问题都搞错, 真是汗颜。 要加紧学习啊, Stay Hungry, Stay Foolish!

Written by oldmonk on 九月 11th, 2006 with 3 comments.
Read more articles on IT.

Tags: ,

Related articles

3 comments

Read the comments left by other users below, or:

引用

Get your own gravatar by visiting gravatar.com 空心人
#1. 九月 13th, 2006, at 1:24 下午.

以前曾有过这个的疑问,赶紧在Linux上试了一下,Segment fault之后也不能被catch住。到网上查了一下,要想catch住这种系统信号,可以在信号处理函数中加上throw语句,并且在用g++编译时,加上non-call-exceptions

Since processor exceptions may occur anywhere in the code, the stack un-
winding libraries must be prepared to handle exceptions within any context.
This functionality is enabled by a special GNU g++ compiler °ag, \-fnon-call-
exceptions". Without the use of this °ag, only explicitly thrown exceptions are
handled. This option is typically used from user-space code that attempts to
throw exceptions from signal handlers. In the kernel, this allows an exception to
be correctly dispatched even in the absence of an explicit throw call.

[回复此评论]

引用

Get your own gravatar by visiting gravatar.com hanbger
#2. 四月 9th, 2007, at 2:26 上午.

:em21: good example~~~

[回复此评论]

引用

Get your own gravatar by visiting gravatar.com tenlywu
#3. 四月 13th, 2009, at 12:12 上午.

刚好写GA代码文章的时候,提到了try catch

[回复此评论]

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




  • :em10:
  • :em01:
  • :em13:
  • :em04:
  • :em05:
  • :em06:
  • :em12:
  • :em09:
  • :em07:
  • :em08:
  • :em21:
  • :em17:
  • :em33:
  • :em03:
  • :em02:
  • :em31:
  • :em34:
  • :em28:
  • :em14:
  • :em32:
  • :em36:
  • :em38:
  • :em16:
  • :em11:
  • :em18:
  • :em20:
  • :em22:
  • :em15:
  • :em19:
  • :em23:
  • :em25:
  • :em24:
  • :em29:
  • :em30:
  • :em27:
  • :em35:
  • :em26:
  • :em56:
  • :em57:
  • :em54:
  • :em37:
  • :em45:
  • :em46:
  • :em42:
  • :em39:
  • :em44:
  • :em51:
  • :em60:
  • :em43:
  • :em40:
  • :em49:
  • :em41:
  • :em47:
  • :em48:
  • :em50:
  • :em55:
  • :em58:
  • :em53:
  • :em52:
  • :em66:
  • :em64:
  • :em68:
  • :em65:
  • :em61:
  • :em59:
  • :em67:
  • :em70:
  • :em71:
  • :em62:
  • :em63:
  • :em69:
  • :em72:

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

 
Web www.doyj.com

Music

Categories

Monthly archives

看我blog的人

Favourite sites

订阅文章

Recent Comments

Most Commented

Random Article

创作共同