-------------------------------------------------------------------------------
1. 利用 windows 的API SetUnhandledExceptionFilter
-------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h> LONG WINAPI UnhandledExceptionFilter2(LPEXCEPTION_POINTERS pep)
{
int code = pep->ExceptionRecord->ExceptionCode;
int flags = pep->ExceptionRecord->ExceptionFlags;
printf("Exception Caught: Code = %i, Flags = %i, Desc = %s\n", code,
flags, GetExceptionDescription(code));
if (flags == EXCEPTION_NONCONTINUABLE)
{
MessageBox(NULL, "Cannot continue; that would only generate another exception!",
"Exception Caught", MB_OK);
return EXCEPTION_EXECUTE_HANDLER;
}
pep->ContextRecord->Eip -= 8;
return EXCEPTION_CONTINUE_EXECUTION;
} int testxcpt()
{
LPTOP_LEVEL_EXCEPTION_FILTER pOldFilter;
char *s = NULL;
int rc = FALSE; pOldFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter2);
printf("Generate exception? y/n: ");
fflush(stdin);
switch(getchar())
{
case 'y':
case 'Y':
*s = *s;
break;
case 'n':
case 'N':
s = "s";
rc = TRUE;
break;
default:
printf("I said enter y or n!\n");
}
SetUnhandledExceptionFilter(pOldFilter); return rc;
} int main()
{
if (testxcpt())
printf("testxcpt() succeeded\n");
else
printf("testxcpt() failed\n");
return 0;
}
-------------------------------------------------------------------------------
2. "excpt.h" - __try1
-------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <excpt.h> char *GetExceptionDescription(LONG code); EXCEPTION_DISPOSITION MyHandler (struct _EXCEPTION_RECORD* er, void* buf, struct _CONTEXT* ctx, void* buf2)
{
printf("ExceptionCode = %08X %s\n"
"ExceptionFlags = %08X\n"
"ContextFlags = %08X\n"
"SegGs = %08X\n"
"SegFs = %08X\n"
"SegEs = %08X\n"
"SegDs = %08X\n"
"Edi = %08X\n"
"Esi = %08X\n"
"Ebx = %08X\n"
"Edx = %08X\n"
"Ecx = %08X\n"
"Eax = %08X\n"
"Ebp = %08X\n"
"Eip = %08X\n"
"SegCs = %08X\n"
"EFlags = %08X\n"
"Esp = %08X\n"
"SegSs = %08X\n",
er->ExceptionCode,
GetExceptionDescription(er->ExceptionCode),
er->ExceptionFlags,
ctx->ContextFlags,
ctx->SegGs,
ctx->SegFs,
ctx->SegEs,
ctx->SegDs,
ctx->Edi,
ctx->Esi,
ctx->Ebx,
ctx->Edx,
ctx->Ecx,
ctx->Eax,
ctx->Ebp,
ctx->Eip,
ctx->SegCs,
ctx->EFlags,
ctx->Esp,
ctx->SegSs
);
return ExceptionNestedException;
} int main(void)
{
__try1(MyHandler)
{
int *p=(int*)0x00001234;
*p=12;
}
__except1;
{
printf("Exception Caught");
}
return 0;
}
-------------------------------------------------------------------------------
3. libseh
-------------------------------------------------------------------------------
LibSEH is a compatibility layer that allows one to utilize the Structured Exception Handling facility found in Windows within GNU C/C++ for Windows (MINGW32, CYGWIN). In other compilers, SEH is built into the compiler as a language extension. In other words, this syntax is not standard C or C++, where standard in this case includes any ANSI standard. Usually, support for this feature is implemented through __try, __except, and __finally compound statements. 在 mingw32 中使用最好 GetExceptionCode() fix to -> GetExceptionCodeSEH()
GetExceptionInformation() fix to -> GetExceptionInformationSEH() #include <windows.h>
#include <stdio.h> /* The LibSEH header needs to be included */
#include <seh.h>
char *GetExceptionDescription(LONG code); int ExceptionFilter(unsigned int code, unsigned int excToFilter)
{
printf("ExceptionCode = %08X %s\n", code, GetExceptionDescription(code) );
if(code == excToFilter) return EXCEPTION_EXECUTE_HANDLER;
else return EXCEPTION_CONTINUE_SEARCH;
} int main()
{
__seh_try
{
int x = 0;
int y = 4;
y /= x;
}
__seh_except(ExceptionFilter(GetExceptionCodeSEH(), EXCEPTION_INT_DIVIDE_BY_ZERO))
{
printf("Divide by zero exception.\n");
}
__seh_end_except __seh_try
{
int *p=(int*)0x00001234;
*p=12;
}
__seh_except(ExceptionFilter(GetExceptionCodeSEH(), EXCEPTION_ACCESS_VIOLATION))
{
printf("Exception Caught.\n");
}
__seh_end_except return 0;
} Note:
http://www.programmingunlimited.net/siteexec/content.cgi?page=libseh
http://www.programmingunlimited.net/files/libseh-0.0.4.zip
-------------------------------------------------------------------------------
4. exceptions4c
-------------------------------------------------------------------------------
exceptions4c is a tiny, portable framework that brings the power of exceptions to your C applications. It provides a simple set of keywords (macros, actually) which map the semantics of exception handling you're probably already used to: try, catch, finally, throw. You can write try/catch/finally blocks just as if you were coding in Java. This way you will never have to deal again with boring error codes, or check return values every time you call a function. If you are using threads in your program, you must enable the thread-safe version of the library by defining E4C_THREADSAFE at compiler level. The usage of the framework does not vary between single and multithreaded programs. The same semantics apply. The only caveat is that the behavior of signal handling is undefined in a multithreaded program so use this feature with caution. 在 mingw32 中, 编译时需要加 -DE4C_THREADSAFE 参数 #include <e4c.h>
#include <stdio.h> int main(void)
{
printf("enter main()\n");
int * pointer = NULL;
int i=0;
e4c_context_begin(E4C_TRUE);
try
{
printf("enter try\n");
int oops = *pointer;
}
catch(RuntimeException)
{
const e4c_exception * exception = e4c_get_exception();
printf("Exception Caught.\n");
if(exception)
{
printf( " exception->name %s\n"
" exception->message %s\n"
" exception->file %s\n"
" exception->line %ld\n"
" exception->function %s\n",
exception->name,
exception->message,
exception->file,
exception->line,
exception->function );
}
}
finally
{
printf("finally.\n");
}
e4c_context_end();
return 0;
} Note:
https://github.com/guillermocalvo/exceptions4c
https://github.com/guillermocalvo/exceptions4c/archive/master.zip <- 3.0.6
https://github.com/guillermocalvo/exceptions4c/archive/v3.0.5.tar.gz

libseh-0.0.4__exceptions4c-3.0.6.7z

mingw32 捕获异常的4种方法的更多相关文章

  1. 读取Excel文件的两种方法

    第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...

  2. (转)Java结束线程的三种方法

    背景:面试过程中问到结束线程的方法和线程池shutdown shutdownnow区别以及底层的实现,当时答的并不好. Java结束线程的三种方法 线程属于一次性消耗品,在执行完run()方法之后线程 ...

  3. Java中终止线程的三种方法

    终止线程一般建议采用的方法是让线程自行结束,进入Dead(死亡)状态,就是执行完run()方法.即如果想要停止一个线程的执行,就要提供某种方式让线程能够自动结束run()方法的执行.比如设置一个标志来 ...

  4. Java结束线程的三种方法(爱奇艺面试)

    线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...

  5. Java结束线程的三种方法

    线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...

  6. Node.js模拟发起http请求从异步转同步的5种方法

    使用Node.js模拟发起http请求很常用的,但是由于Node模块(原生和第三方库)提供里面的方法都是异步,对于很多场景下应用很麻烦,不如同步来的方便.下面总结了几个常见的库API从异步转同步的几种 ...

  7. ASP.Net Core中处理异常的几种方法

    本文将介绍在ASP.Net Core中处理异常的几种方法 1使用开发人员异常页面(The developer exception page) 2配置HTTP错误代码页 Configuring stat ...

  8. JS 判断数据类型的三种方法

    说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...

  9. DataTable 转换成 Json的3种方法

    在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...

随机推荐

  1. java并发编程学习: 原子变量(CAS)

    先上一段代码: package test; public class Program { public static int i = 0; private static class Next exte ...

  2. Office2013插件开发Outlook篇(2)-- Ribbon

    一.获取当前实例 在Ribbon1的任何方法中调用如下代码,可获取当前实例. 如: Application application = new Application(); var list = ap ...

  3. win7 cmd 操作mysql数据库

    一 ,对MySql服务器的开启,重启,关闭等操作       当然,可以在win7的界面环境下,关闭或开启MySql服务.但是经常找不到win7的服务管理器,主要定位方法有二:命令行下输入servic ...

  4. AppBox升级进行时 - Any与All的用法(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 属于某个角色的用户列表(Any的用法) 使用Subsonic,我们有两种方法获 ...

  5. Linq to Xml读取复杂xml(带命名空间)

    前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...

  6. MVC之前的那点事儿系列(8):UrlRouting的理解

    文章内容 根据对Http Runtime和Http Pipeline的分析,我们知道一个ASP.NET应用程序可以有多个HttpModuel,但是只能有一个HttpHandler,并且通过这个Http ...

  7. 用html5 canvas和JS写个数独游戏

    为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...

  8. jquey on

    1.如果你的元素是用clone方法复制出来的,并且,用了on来绑定事件的话,必须在clone的后边添加true,负责你的事件不会生效. 2.必须在on $('.js-liubody').on('cli ...

  9. iOS-不用网线搭建IPv6网络测试环境

    前言 从6月1日开始苹果要求之后审核的项目必须支持iPv6,如果不支持将被拒绝,掘金最近一次审核被就被拒绝了....理由为下: Apps are reviewed on an IPv6 network ...

  10. HDU 1817Necklace of Beads(置换+Polya计数)

    Necklace of Beads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...