VS配置将崩溃转化为异常
来源:互联网 发布:沙特王储知乎 编辑:程序博客网 时间:2024/06/04 19:32
相关资料链接:http://msdn.microsoft.com/zh-cn/library/1deeycx5%28v=VS.80%29.aspx
在调用系统API或其他的异常引起程序崩溃终止,可以在VS的工程配置异常选项,将引起程序崩溃的异常转化为用try_catch可以处理的异常。
设置位置 Project Property Pages-> Configuration Properties -> C/C++->Code Generation-> Enable C++ Exceptions -> Yes with SEH Exception (/EHa)
异常捕获含义:
- EHa 捕获异步(结构化)异常和同步 (C++) 异常的异常处理模型。
- EHs 仅捕获 C++ 异常并通知编译器假定 extern C 函数确实引发了异常的异常处理模型。
- EHc 如果与 s (/EHsc) 一起使用,则仅捕获 C++ 异常并通知编译器假定extern C 函数从未引发 C++ 异常。/EHca 等效于/EHa。
同时也可以使用Windows的API函数 _set_se_translator,捕获异常后进行处理
相关链接:http://technet.microsoft.com/zh-cn/subscriptions/5z4bw5h5%28v=vs.71%29.aspx
_set_se_translator
Handles Win32 exceptions (C structured exceptions) as C++ typed exceptions.
_se_translator_function _set_se_translator( _se_translator_function se_trans_func );
Parameter
- se_trans_func
- Pointer to a C structured exception translator function that you write.
Return Value
Returns a pointer to the previous translator function registered by _set_se_translator, so that the previous function can be restored later. If no previous function has been set, the return value may be used to restore the default behavior; this value may be NULL.
Remarks
The _set_se_translator function provides a way to handle Win32 exceptions (C structured exceptions) as C++ typed exceptions. To allow each C exception to be handled by a C++catch handler, first define a C exception wrapper class that can be used, or derived from, to attribute a specific class type to a C exception. To use this class, install a custom C exception translator function that is called by the internal exception-handling mechanism each time a C exception is raised. Within your translator function, you can throw any typed exception that can be caught by a matching C++catch handler.
Use /EHa instead of /EHsc when using _set_se_translator.
To specify a custom translation function, call _set_se_translator with the name of your translation function as its argument. The translator function that you write is called once for each function invocation on the stack that hastry blocks. There is no default translator function.
In a multithreaded environment, translator functions are maintained separately for each thread. Each new thread needs to install its own translator function. Thus, each thread is in charge of its own translation handling._set_se_translator is specific to one thread; another DLL can install a different translation function.
The se_trans_func function that you write must take an unsigned integer and a pointer to a Win32_EXCEPTION_POINTERS structure as arguments. The arguments are the return values of calls to the Win32 APIGetExceptionCode and GetExceptionInformation functions, respectively.
typedef void (*_se_translator_function)(unsigned int, struct _EXCEPTION_POINTERS* );
For _set_se_translator, there are implications when dynamically linking to the CRT; another DLL in the process may call_set_se_translator and replace your handler with its own.
Requirements
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_settrans.cpp// compile with: /EHsc#include <stdio.h>#include <windows.h>#include <eh.h>void SEFunc();void trans_func( unsigned int, EXCEPTION_POINTERS* );class SE_Exception{private: unsigned int nSE;public: SE_Exception() {} SE_Exception( unsigned int n ) : nSE( n ) {} ~SE_Exception() {} unsigned int getSeNumber() { return nSE; }};int main( void ){ try { _set_se_translator( trans_func ); SEFunc(); } catch( SE_Exception e ) { printf( "Caught a __try exception with SE_Exception.\n" ); }}void SEFunc(){ __try { int x, y=0; x = 5 / y; } __finally { printf( "In finally\n" ); }}void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp ){ printf( "In trans_func.\n" ); throw SE_Exception();}
Output
In trans_func.In finallyCaught a __try exception with SE_Exception.
- VS配置将崩溃转化为异常
- 将“受检查异常”转化为“不受检查异常”
- 使用VS将sqlite3.def转化为sqlite3.lib
- 将gdal源码转化为VS工程编译过程记录
- SEH转化为C++异常
- 将Ip转化为十进制数字,将十进制数字转化为Ip
- Spring MVC+Jackson+Hibernate将一个List转化为JSON异常
- jaxb 将xml转化为bean时报异常javax.xml.bind.UnmarshalException: unexpected element
- Java 如何将String转化为Int及java.lang.NumberFormatException异常处理
- 将bmp 转化为avi
- 将体能转化为智能
- 将字符串转化为日期
- 将数字转化为中文
- 将数字转化为中文
- 将DateRead转化为DataSet
- 将数字转化为中文
- 将汉字转化为字母
- 将IntPtr转化为byte[]
- 第三周 项目二旱冰场造价
- 如何做好项目需求
- 多线程高级的工具
- C# SQL 数据库连接方式
- OnCreate函数
- VS配置将崩溃转化为异常
- 选择,插入,希尔,快速,堆,归并排序六种排序方式的Java 实现和性能对比(付代码)
- 腾讯京东联姻的1个洗牌信号:移动电商元年
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- android播放http流媒体时默认最低最高缓冲值在4.0.3版本上进行了修改
- 1044: [HAOI2008]木棍分割 题解
- 分享java读写Properties文件
- 盈一指风华,浅梦一程。生命的美丽
- 通过这次战略合作,腾讯和京东这两家领先的互联网公司将组建更强大的团队