大家好,欢迎来到IT知识分享网。
严格的来说,那不算是一篇完整的文章,更多的是提出我的疑惑。顺便总结了一下网友关于C++异常捕获及异常处理给出的精炼的示例。
至今,上文提到的疑惑本菜鸟都没有完全解开。
于是,我就选择了用 __try __except 来捕获及处理异常。经过测试,我想捕获的异常用 __try __except 都捕获到了,相当开心。
但是,今天在用 __try __except 的时候蹦出来一个让我既苦恼又兴奋的错误:
error C2712: 无法在要求对象展开的函数中使用__try
本能的打开百度,输入错误提示,一顿查找,并没有找到很好理解的解释。于是我求救 google ,终于找到了我能很容易理解的解释。(哎,小学语文没学好就是吃亏)
首先,我们来看几个会报C2712错误的示例:
上述代码在编译的时候会报C2712错误,原因在代码注释中简单注明了。
其实原因就是:
在使用 __try __except 的函数中任何位置(测试的几个位置都会报错,如有描述错误请告知)创建了类对象就会导致C2712编译错误。
///
msdn上给出的描述和解决方案
When you use /EHsc, a function with structured exception handling cannot have objects that require unwinding (destruction).
Possible solutions:
Move code that requires SEH to another function
Rewrite functions that use SEH to avoid the use of local variables and parameters that have destructors. Do not use SEH in constructors or destructors
Compile without /EHsc
Error C2712 can also occur if you call a method declared by using the __event keyword. Because the event might be used in a multithreaded environment, the compiler generates code that prevents manipulation of the underlying event object, and then encloses the generated code in an SEH try-finally statement. Consequently, error C2712 will occur if you call the event method and pass by value an argument whose type has a destructor. One solution in this case is to pass the argument as a constant reference.
//
我们来将上面三个函数和 main 修改一下:
所以我们再修改一下,把 main 拆成两个函数:
这样,所有问题就解决啦。
纯手打,如果有什么问题,欢迎各位大佬指出。
好了,我给大佬递茶去了。。。
声明:上述代码未包含所有需要的头文件,请大家自行脑补。
参考:
Compiler Error C2712
very simple code, and getting error C2712, could not understand why
版权声明
如需转载请注明来源和作者,谢谢!!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/129517.html