1. // testFinally.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. #include <windows.h>
  7. std::string s;
  8. //本源码在VC2008+SP1 WindowsXP+SP3 下测试通过 by lee353086
  9. void myFinally()
  10. {
  11. int i=2;//"int i",不需要析构所以可以放在这里
  12. //注意"std::string s;"这行代码不能放在
  13. //这里,因为s这个对象需要析构
  14. //除0,非法指令、存取违例 等等对C++而言是 错误,而不是 异常,和 try-catch 无关
  15. //所以这里得使用__try__except来捕获除零错误
  16. __try
  17. {
  18. s="My Finally";
  19. //throw s;//这里throw的s异常会被_tmain中的catch(std::string s)捕获,myFinally函数体剩余的代码将不会被执行
  20. i = i/0;
  21. }
  22. //下面这行可以用用__except(1)来代替,这样就不需要包含#include <windows.h>这段代码
  23. __except( GetExceptionCode()==EXCEPTION_INT_DIVIDE_BY_ZERO ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH )
  24. {
  25. cout<<"got除零错误!"<<endl;
  26. }
  27. cout<<s.c_str()<<endl;
  28. }
  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31. cout<<"main begin"<<endl;
  32. try{
  33. myFinally();
  34. }catch(std::string s)
  35. {
  36. cout<<"got其它错误"<<endl;
  37. }
  38. cout<<"main end"<<endl;
  39. return 0;
  40. }
http://blog.csdn.net/lee353086/article/details/6365998

最新文章

  1. POJ 1966 Cable TV Network
  2. js 函数
  3. io.js入门(二)—— 所支持的ES6(上)
  4. extjs学习资料
  5. 我所了解的各公司使用的 Ceph 存储集群 (携程、乐视、有云、宝德云、联通等)
  6. [转]JS中对象与字符串的互相转换
  7. Construct Bounding Sphere
  8. Android 如何解决数据库多线程锁的问题
  9. Windows 远程停止iis服务
  10. Loader之二:CursorLoader基本实例
  11. EcShop后台添加菜单[步骤]
  12. XSS分析及如何预防
  13. GitLab配置ssh key
  14. javascript中apply、call和bind的区别及方法详解
  15. Dapper批量操作实体
  16. 码云 git sourceTree 私有
  17. day24 包
  18. MyEclipse上有main函数类运行报错:Editor does not contain a main type
  19. 美团点评基于 Flink 的实时数仓建设实践
  20. work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章

热门文章

  1. vue学习笔记二:v-if和v-show的区别
  2. 【例题5-3 UVA - 10815】Andy&#39;s First Dictionary
  3. cocos2d-x游戏开发 跑酷(九) 源代码下载及小结
  4. Activity生命周期的回调,你应该知道得很多其它!--Android源代码剖析(下)
  5. 中小研发团队架构实践之RabbitMQ快速入门及应用
  6. Android Widget和悬浮窗 原理
  7. FAST特征点检测&&KeyPoint类
  8. SSH连接Linux的Server超时
  9. 解析C#内存管理
  10. 【p081】ISBN号码