• VC除零异常(错误)捕获



    [cpp]
     view plain copy
     
    1. // testFinally.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. #include <iostream>  
    7. using namespace std;  
    8.   
    9. #include <windows.h>  
    10.   
    11. std::string s;  
    12.   
    13. //本源码在VC2008+SP1 WindowsXP+SP3 下测试通过 by lee353086  
    14.   
    15. void myFinally()  
    16. {     
    17.     int i=2;//"int i",不需要析构所以可以放在这里  
    18.   
    19.     //注意"std::string s;"这行代码不能放在  
    20.     //这里,因为s这个对象需要析构  
    21.       
    22.      //除0,非法指令、存取违例 等等对C++而言是 错误,而不是 异常,和 try-catch 无关  
    23.     //所以这里得使用__try__except来捕获除零错误  
    24.     __try  
    25.     {  
    26.         s="My Finally";  
    27.         //throw s;//这里throw的s异常会被_tmain中的catch(std::string s)捕获,myFinally函数体剩余的代码将不会被执行  
    28.         i = i/0;  
    29.     }  
    30.     //下面这行可以用用__except(1)来代替,这样就不需要包含#include <windows.h>这段代码  
    31.     __except( GetExceptionCode()==EXCEPTION_INT_DIVIDE_BY_ZERO ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH )  
    32.     {  
    33.         cout<<"got除零错误!"<<endl;  
    34.     }  
    35.     cout<<s.c_str()<<endl;  
    36. }  
    37.   
    38. int _tmain(int argc, _TCHAR* argv[])  
    39. {  
    40.     cout<<"main begin"<<endl;  
    41.   
    42.     try{  
    43.         myFinally();  
    44.     }catch(std::string s)  
    45.     {  
    46.         cout<<"got其它错误"<<endl;  
    47.     }  
    48.   
    49.     cout<<"main end"<<endl;  
    50.   
    51.     return 0;  
    52. }  
    http://blog.csdn.net/lee353086/article/details/6365998
  • 相关阅读:
    【数据删除】树
    线段树分治 学习笔记
    带删除并查集 学习笔记
    抖音极速版2022下载 Elon
    抖音极速版下载 Elon
    抖音下载 Elon
    什么是Optional【JDK 8特性】 Elon
    java8 stream().map().collect()用法
    vuex的使用
    Java发送简单email:SimpleEmail
  • 原文地址:https://www.cnblogs.com/findumars/p/7635901.html
Copyright © 2020-2023  润新知