windows异常演示,指定异常类型,然后生成异常
#include "stdafx.h"
#include <Windows.h>
#include <float.h>
DWORD Filter (LPEXCEPTION_POINTERS, LPDWORD);
double x = 1.0, y = 0.0;
int _tmain (int argc, LPTSTR argv[])
{
DWORD eCategory, i = 0, ix, iy = 0;
LPDWORD pNull = NULL;
BOOL done = FALSE;
unsigned int fpOld, fpNew, fpOldDummy;
//__try { /* Try-Finally block. */
/* Save old control mask. */
_controlfp_s (&fpOld, 0, 0);
/* Enable floating-point exceptions. */
fpNew = fpOld & ~(EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT
| EM_ZERODIVIDE | EM_DENORMAL | EM_INVALID);
/* Set new control mask. */
_controlfp_s (&fpOldDummy, fpNew, MCW_EM);
while (!done) __try {
_tprintf (_T("Enter exception type:\n"));
_tprintf (_T("1: Mem, 2: Int, 3: Flt 4: User 5: _leave 6: return\n"));
_tscanf_s (_T("%d"), &i);
__try { /* Try-Except block. */
switch (i) {
case 1: /* Memory reference. */
ix = *pNull;
*pNull = 5;
break;
case 2: /* Integer arithmetic. */
ix = ix / iy;
break;
case 3: /* floating-point exception. */
x = x / y;
_tprintf (_T("x = %20e\n"), x);
break;
case 4: /* User generated exception. */
//ReportException (_T("Raising user exception.\n"), 1); //这个暂时没有写
break;
case 5: /* Use the _leave statement to terminate. */
done = TRUE;
__leave;
case 6: /* Use the return statement to terminate. */
return 1;
default: done = TRUE;
}
} /* End of inner __try. */
__except (Filter (GetExceptionInformation (), &eCategory)){
switch (eCategory) {
case 0: _tprintf (_T("Unknown exception.\n"));
break;
case 1: _tprintf (_T("Memory ref exception.\n"));
break;
case 2: _tprintf (_T("Integer arithmetic exception.\n"));
break;
case 3:
_tprintf (_T("floating-point exception.\n"));
break;
case 10: _tprintf (_T("User generated exception.\n"));
break;
default: _tprintf (_T("Unknown exception.\n"));
break;
}
_tprintf (_T("End of handler.\n"));
} /* End of inner __try __except block. */
//} /* End of exception generation loop. */
//return; /* Cause an abnormal termination. */
} /* End of outer __try __finally */
__finally {
BOOL AbTerm; /* Restore the old mask value. */
_controlfp_s (&fpOldDummy, fpOld, MCW_EM);
AbTerm = AbnormalTermination();
_tprintf (_T("Abnormal Termination?: %d\n"), AbTerm);
}
return 0;
}
static DWORD Filter (LPEXCEPTION_POINTERS pExP, LPDWORD eCategory)
/* Categorize the exception and decide whether to continue execution or
execute the handler or to continue the search for a handler that
can process this exception type. The exception category is only used
by the exception handler. */
{
DWORD exCode;
DWORD_PTR readWrite, virtAddr;
exCode = pExP->ExceptionRecord->ExceptionCode;
_tprintf (_T("Filter. exCode: %x\n"), exCode);
if ((0x20000000 & exCode) != 0) {
/* User Exception. */
*eCategory = 10;
return EXCEPTION_EXECUTE_HANDLER;
}
switch (exCode) {
case EXCEPTION_ACCESS_VIOLATION:
/* Determine whether it was a read, write, or execute
and give the virtual address. */
readWrite =
(DWORD)(pExP->ExceptionRecord->ExceptionInformation [0]);
virtAddr =
(DWORD)(pExP->ExceptionRecord->ExceptionInformation [1]);
_tprintf
(_T("Access Violation. Read/Write/Execute: %d. Address: %x\n"),
readWrite, virtAddr);
*eCategory = 1;
return EXCEPTION_EXECUTE_HANDLER;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
*eCategory = 1;
return EXCEPTION_EXECUTE_HANDLER;
/* Integer arithmetic exception. Halt execution. */
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_INT_OVERFLOW:
*eCategory = 2;
return EXCEPTION_EXECUTE_HANDLER;
/* Float exception. Attempt to continue execution. */
/* Return the maximum floating value. */
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_OVERFLOW:
_tprintf (_T("Flt Exception - Large result.\n"));
*eCategory = 3;
_clearfp();
return EXCEPTION_EXECUTE_HANDLER;
case EXCEPTION_FLT_DENORMAL_OPERAND:
case EXCEPTION_FLT_INEXACT_RESULT:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_FLT_STACK_CHECK:
_tprintf (_T("Flt Exception - Unknown result.\n"));
*eCategory = 3;
return EXCEPTION_CONTINUE_EXECUTION;
/* Return the minimum floating value. */
case EXCEPTION_FLT_UNDERFLOW:
_tprintf (_T("Flt Exception - Small result.\n"));
*eCategory = 3;
return EXCEPTION_CONTINUE_EXECUTION;
case EXCEPTION_DATATYPE_MISALIGNMENT:
*eCategory = 4;
return EXCEPTION_CONTINUE_SEARCH;
case STATUS_NONCONTINUABLE_EXCEPTION:
*eCategory = 5;
return EXCEPTION_EXECUTE_HANDLER;
case EXCEPTION_ILLEGAL_INSTRUCTION:
case EXCEPTION_PRIV_INSTRUCTION:
*eCategory = 6;
return EXCEPTION_EXECUTE_HANDLER;
case STATUS_NO_MEMORY:
*eCategory = 7;
return EXCEPTION_EXECUTE_HANDLER;
default:
*eCategory = 0;
return EXCEPTION_CONTINUE_SEARCH;
}
}
运行结果如下:
windows异常演示,指定异常类型,然后生成异常的更多相关文章
- springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。为了区别不同的异常通常根据异常类型自定义异常类,这里我们创建一个自定义系统异常,如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。
springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑. 1.1 异常处理思路 系统中异常包括两类:预期异常和运行时异常RuntimeEx ...
- C# Webservice 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问
摘自: http://blog.csdn.net/gulijiang2008/article/details/4482993 请在服务器端配置 方法一: 在通过WebService处理大数据量数据时出 ...
- “DBUtility.DbHelperSQL”的类型初始值设定项引发异常 “DBUtility.DbHelperSQL”的类型初始值设定项引发异常
今天遇到了一个这样的问题“DBUtility.DbHelperSQL”的类型初始值设定项引发异常“DBUtility.DbHelperSQL”的类型初始值设定项引发异常 也许有和我遇到这问题的人也在这 ...
- MVC项目,系统找不到指定的文件。(异常来自 HRESULT:0x80070002)
今天在用Visual Studio新建MVC项目的时候,遇到错误 系统找不到指定的文件.(异常来自 HRESULT:0x80070002) 解决办法:工具--> 扩展和更新 -->联机(V ...
- WPF学习笔记——在“System.Windows.StaticResourceExtension”上提供值时引发了异常
在"System.Windows.StaticResourceExtension"上提供值时引发了异常 因应需要,写了一个转换器,然后窗体上引用,结果就出来这个错.编译的时候没事, ...
- 类型信息、异常、I/O流(day11)
二十三 运行时的类型信息 typeid运算符 #include <typeinfo> typeid(类型/对象) )返回typeinfo的对象,用于描述类型信息. )在typeinfo类中 ...
- Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别
一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕 ...
- Spring MVC异常统一处理(包括普通请求异常以及ajax请求异常)
通常SpringMVC对异常的配置都是返回某个jsp视图给用户,但是通过ajax方式发起请求,即使发生异常,前台也无法获得任何异常提示信息.因此需要对异常进行统一的处理,对于普通请求以及ajax请求的 ...
- Atitit 异常的实现原理 与用户业务异常
Atitit 异常的实现原理 与用户业务异常 1.1. 异常的实现原理1 1.2. 用户业务异常1 1.3. 异常转译和异常链2 1.4. 避免异常2 1.5. 异常恢复3 1.6. catch代码块 ...
- 应该抛出什么异常?不应该抛出什么异常?(.NET/C#)
我在 .NET/C# 建议的异常处理原则 中描述了如何 catch 异常以及重新 throw.然而何时应该 throw 异常,以及应该 throw 什么异常呢? 究竟是谁错了? 代码中从上到下从里到外 ...
随机推荐
- jq点击切换按钮最简洁代码
<div id="swphoto"> <img src="1.jpg"> <img src="2.jpg&q ...
- HDU3047 Zjnu Stadium
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- hdoj1002--A + B Problem II
Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...
- audiojs 音频插件使用教程
audiojs 音频插件使用教程 github地址 https://kolber.github.io/audiojs/ 依赖文件 <script src="https://cdn.bo ...
- c、rust、golang、swift性能比较
mac 计算速度视觉判断是(由好到差):c > rust > swift > golang 内存开销在mac上是(由好到差):c > rust > golang > ...
- cocos2d-js 创建帧动画
封装创建方法 var RunActionHelper = function(){ };RunActionHelper.prototype.createAnimationByPlist = functi ...
- jquery判断密码是否一致?
密码 请输入密码 重新输入密码 请输入新密码 <input type="text" id="btn0"> 密码 <span class=&qu ...
- Python3.6.0安装
1.安装 具体详情请参考下图: 双击安装包: 勾选“add python 3.6 to PATH”这样可以自动生成环境变量,选择“Customize installation”自定义安装. 2. ...
- nyoj-1278-Prototypes analyze(二叉排序树模板)
题目链接 思路:建树之后,判断有多少种不同的树. 判断不同的树,简单的思路是遍历数组,判断数组后面是否存在一样的树 /* Name:NYOJ-1278-Prototypes analyze Copyr ...
- New Concept English three (59)
24 45 People tend to amass possessions, sometimes without being aware of doing so. Indeed they can h ...