VC除零异常(错误)捕获
2024-09-28 05:57:38
- // testFinally.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #include <windows.h>
- std::string s;
- //本源码在VC2008+SP1 WindowsXP+SP3 下测试通过 by lee353086
- void myFinally()
- {
- int i=2;//"int i",不需要析构所以可以放在这里
- //注意"std::string s;"这行代码不能放在
- //这里,因为s这个对象需要析构
- //除0,非法指令、存取违例 等等对C++而言是 错误,而不是 异常,和 try-catch 无关
- //所以这里得使用__try__except来捕获除零错误
- __try
- {
- s="My Finally";
- //throw s;//这里throw的s异常会被_tmain中的catch(std::string s)捕获,myFinally函数体剩余的代码将不会被执行
- i = i/0;
- }
- //下面这行可以用用__except(1)来代替,这样就不需要包含#include <windows.h>这段代码
- __except( GetExceptionCode()==EXCEPTION_INT_DIVIDE_BY_ZERO ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH )
- {
- cout<<"got除零错误!"<<endl;
- }
- cout<<s.c_str()<<endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout<<"main begin"<<endl;
- try{
- myFinally();
- }catch(std::string s)
- {
- cout<<"got其它错误"<<endl;
- }
- cout<<"main end"<<endl;
- return 0;
- }
http://blog.csdn.net/lee353086/article/details/6365998
最新文章
- POJ 1966 Cable TV Network
- js 函数
- io.js入门(二)—— 所支持的ES6(上)
- extjs学习资料
- 我所了解的各公司使用的 Ceph 存储集群 (携程、乐视、有云、宝德云、联通等)
- [转]JS中对象与字符串的互相转换
- Construct Bounding Sphere
- Android 如何解决数据库多线程锁的问题
- Windows 远程停止iis服务
- Loader之二:CursorLoader基本实例
- EcShop后台添加菜单[步骤]
- XSS分析及如何预防
- GitLab配置ssh key
- javascript中apply、call和bind的区别及方法详解
- Dapper批量操作实体
- 码云 git sourceTree 私有
- day24 包
- MyEclipse上有main函数类运行报错:Editor does not contain a main type
- 美团点评基于 Flink 的实时数仓建设实践
- work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章