zoukankan      html  css  js  c++  java
  • 测试 __try, __finally, __except(被__finally捕获的异常, 还会被上一级的__except捕获。反之不行)

    C语言标准是没有 try-catch语法 的, M$家自己提供了一组.

    [cpp] view plain copy
     
    1. /// @file ClassroomExamples.c  
    2. /// @brief 验证C语言的非标准try, catch  
    3.   
    4. #include <windows.h>  
    5. #include <stdlib.h>  
    6. #include <stdio.h>  
    7. #include <stddef.h>  
    8. #include <crtdbg.h>  
    9. #include <conio.h>  
    10.   
    11. void fnTest_TryCatchByM$();  
    12.   
    13. int main(int argc, char *argv[ ], char *envp[ ])  
    14. {  
    15.     fnTest_TryCatchByM$();  
    16.     printf("END, press any key to quit ");  
    17.     getchar();  
    18.       
    19.     return 0;  
    20. }  
    21.   
    22. void fnTest_TryCatchByM$()  
    23. {  
    24.     int* p = 0x00000000;   // pointer to NULL  
    25.     puts("hello");  
    26.   
    27.     /// __try, __finally, __except都不是C标准函数, 是M$自己家的  
    28.     /// 为了通用性, 还是不用__x函数  
    29.     /// __try要有配对的__finally或__except, 只能配对一个  
    30.     __try  
    31.     {  
    32.         puts("in try");  
    33.         __try  
    34.         {  
    35.             puts("in try");  
    36.             /// 如果不在__try中, 就C05了  
    37.             *p = 13;    // causes an access violation exception;  
    38.         }  
    39.         __finally  
    40.         {  
    41.             /// 被__finally捕获的异常, 还会被上一级的__except捕获  
    42.             /// 反之不行(先被__except捕获, 不会再被上一级的__finally捕获)  
    43.             puts("in finally");  
    44.         }  
    45.     }  
    46.       
    47.     /// 当异常发生时, 至少要被一个__except捕获, 否则C05  
    48.     /// 进不了对应的__finally  
    49.     /// 最外层的异常捕获必须是__except, 如果是__finally  
    50.     /// 否则异常发生时, 会C05  
    51.     __except(puts("in filter"), 1)  
    52.     {  
    53.         puts("in except");  
    54.     }  
    55.   
    56.     puts("world");  
    57. }  

    http://blog.csdn.net/lostspeed/article/details/50438020

  • 相关阅读:
    Atitit.加密算法ati Aes的框架设计
    Atitit.加密算法ati Aes的框架设计
    Atitit.分布式远程调用  rpc  rmi  CORBA的关系
    Atitit.分布式远程调用  rpc  rmi  CORBA的关系
    Atitit.事件机制 与 消息机制的联系与区别
    Atitit.事件机制 与 消息机制的联系与区别
    Atitit  godaddy 文件权限 root权限设置
    Atitit  godaddy 文件权限 root权限设置
    Atitit.atiRI  与 远程调用的理论and 设计
    Atitit.atiRI  与 远程调用的理论and 设计
  • 原文地址:https://www.cnblogs.com/findumars/p/5187274.html
Copyright © 2011-2022 走看看