⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptionflow.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
//
// FILE: ExceptionFlow.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>

// The g_fShouldFault global variable is set in main(), and read
// in BrokenFunction to determine whether or not
// BrokenFunction should fault.
//
BOOL g_fShouldFault = FALSE;

// The g_fShouldRetry global variable controls whether the
// ImperviousFunctionExceptionFilter evaluates to EXCEPTION_CONTINUE_EXECUTION
// or EXCEPTION_EXECUTE_HANDLER.  g_fShouldRetry is initially TRUE, causing
// ImperviousFunctionExceptionFilter to return EXCEPTION_CONTINUE_EXECUTION
// just once for demonstration purposes.
//
BOOL g_fShouldRetry = TRUE;

// BrokenFunctionExceptionFilter
//
// The exception filter function used by BrokenFunctionException
// (below) to demonstrate EXCEPTION_CONTINUE_SEARCH.
//
int BrokenFunctionExceptionFilter( void )
{
    printf("BrokenFunctionExceptionFilter called - returning EXCEPTION_CONTINUE_SEARCH.\n");
    return(EXCEPTION_CONTINUE_SEARCH);
}

// BrokenFunction
//
// A buggy function that sometimes faults.
//
void BrokenFunction( void )
{
    printf("BrokenFunction being executed.\n");

    __try {
        if( g_fShouldFault )
        {    
            *((char *)0) = '!';
        }
    }
    __except( BrokenFunctionExceptionFilter() ) {
        printf("BrokenFunction's __except handler executing.\n");
    }
    
    printf("BrokenFunction returning.\n");
}

// FastidiousFunction
//
// A very tidy function that always cleans up, even
// if an exception occurs in a function called by
// FastidiousFunction.
//
void FastidiousFunction( void )
{
    printf("FastidiousFunction being executed.\n");

    char *pSomeMemory = new char[1024];

    __try {
        printf("FastidiousFunction about to call BrokenFunction.\n");
        BrokenFunction();
        printf("FastidiousFunction done calling BrokenFunction.\n");
    }
    __finally {
        printf(
            "FastidiousFunction's __finally executing (%s termination).\n",
            AbnormalTermination() ? "abnormal" : "normal"
        );

        delete [] pSomeMemory;
    }

    printf("FastidiousFunction returning.\n");
}

// ImperviousFunctionExceptionFilter
//
// The exception filter function used by ImperviousFunction
// (below) to demonstrate EXCEPTION_EXECUTE_HANDLER.
//
int ImperviousFunctionExceptionFilter( void )
{
    int iFilterEvaluation;

    if( g_fShouldRetry )
    {
        printf("ImperviousFunctionExceptionFilter called - returning EXCEPTION_CONTINUE_EXECUTION.\n");
        iFilterEvaluation = EXCEPTION_CONTINUE_EXECUTION;
        g_fShouldRetry = FALSE;
    }
    else
    {
        printf("ImperviousFunctionExceptionFilter called - returning EXCEPTION_EXECUTE_HANDLER.\n");
        iFilterEvaluation = EXCEPTION_EXECUTE_HANDLER;
    }

    return(iFilterEvaluation);
}

// ImperviousFunction
//
// A bullet proof function that never allows an exception to
// go unhandled.  Exceptions that occur anywhere in functions
// called by ImperviousFunction will never escape
// ImperviousFunction unhandled.
//
void ImperviousFunction( void )
{
    printf("ImperviousFunction being executed.\n");

    __try {
        printf("ImperviousFunction about to call FastidiousFunction\n");
        FastidiousFunction();
        printf("ImperviousFunction done calling FastidiousFunction\n");
    }
    __except( ImperviousFunctionExceptionFilter() ) {
        printf("ImperviousFunction handling exception - nothing can hurt me!\n");
    }

    printf("Returning from ImperviousFunction\n");
}

void main( int iNumArgs )
{
    printf("Program starting.\n");

    // If any command line arguments are passed to this program, the
    // BrokenFunction function will fault.  Otherwise, BrokenFunction
    // will not.
    //
    g_fShouldFault = BOOL(iNumArgs > 1);

    printf("BrokenFunction %s fault.\n", (g_fShouldFault ? "will" : "will not"));

    ImperviousFunction();

    printf("Program exiting.\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -