如果软件中存在问题,而又没有相应的异常处理代码,该异常会最后交给一个默认的异常处理程序处理,系统最后会弹出一个异常对话框。
问题是:我希望自己能够处理这种没有异常处理代码的异常,那么我需要写一个异常处理代码,替换掉系统默认的异常处理程序。根据资料,有一个API函数SetUnhandleExceptionFilter可以实现上述的目的,但不知道怎么用,那位大侠用
过的给我指点一下好么。
另外,还有一个函数SetErrorMode函数,可以保证软件发生未处理异常的情况下不
弹出错误对话框,可是,我试了它各种参数好像都没有用啊。

解决方案 »

  1.   

    TRY
    {
    }
    CATCH()
    {
    }
    finally
    {
    }
      

  2.   

    就是这样做啊,每个可能发生异常的地方都要try一下的TRY
    {
    }
    CATCH()
    {
    }
    finally
    {
    }你的代码放到try里,然后catch可能出现的错误
    把错误处理的代码放到catch里面写finally可有可无,无论程序是否发生异常,finally里的代码都是肯定执行的^_^
      

  3.   

    SetUnhandledExceptionFilter是系统给你处理的最后一次机会,参数就是压入处理函数的地址。
    The SetUnhandledExceptionFilter function lets an application supersede the top-level exception handler that Win32 places at the top of each thread and process. After calling this function, if an exception occurs in a process that is not being debugged, and the exception makes it to the Win32 unhandled exception filter, that filter will call the exception filter function specified by the lpTopLevelExceptionFilter parameter. LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter(    LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter  // exception filter function  
       );
     ParameterslpTopLevelExceptionFilterSupplies the address of a top-level exception filter function that will be called whenever the UnhandledExceptionFilter function gets control, and the process is not being debugged. A value of NULL for this parameter specifies default handling within UnhandledExceptionFilter. The filter function has syntax congruent to that of UnhandledExceptionFilter: It takes a single parameter of type LPEXCEPTION_POINTERS, and returns a value of type LONG. The filter function should return one of the following values: Value Meaning
    EXCEPTION_EXECUTE_HANDLER Return from UnhandledExceptionFilter and execute the associated exception handler. This usually results in process termination.
    EXCEPTION_CONTINUE_EXECUTION Return from UnhandledExceptionFilter and continue execution from the point of the exception. Note that the filter function is free to modify the continuation state by modifying the exception information supplied through its LPEXCEPTION_POINTERS parameter.
    EXCEPTION_CONTINUE_SEARCH Proceed with normal execution of UnhandledExceptionFilter . That means obeying the SetErrorMod flags, or invoking the Application Error pop-up message box.
     Return ValuesThe SetUnhandledExceptionFilter function returns the address of the previous exception filter established with the function. A NULL return value means that there is no current top-level exception handler. ResIssuing SetUnhandledExceptionFilter replaces the existing top-level exception filter for all existing and all future threads in the calling process. 
    The exception handler specified by lpTopLevelExceptionFilter is executed in the context of the thread that caused the fault. This can affect the exception handler's ability to recover from certain exceptions, such as an invalid stack. See AlsoUnhandledExceptionFilter