c語言處理異常 二 -----try--catch
阿新 • • 發佈:2019-02-17
#include "stdio.h" #include "windows.h" int exception_access_violation_filter(LPEXCEPTION_POINTERS p_exinfo) { if(p_exinfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { printf("儲存保護異常\n"); return 1; } else return 0; } int exception_int_divide_by_zero_filter(LPEXCEPTION_POINTERS p_exinfo) { if(p_exinfo->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) { printf("被0除異常\n"); return 1; } else return 0; } void main() { puts("hello"); __try { __try { int* p; // 下面將導致一個異常 p = 0; *p = 45; } // 注意,__except模組捕獲一個儲存保護異常 __except(exception_access_violation_filter(GetExceptionInformation())) { puts("內層的except塊中"); } } // 注意,__except模組捕獲一個被0除異常 __except(exception_int_divide_by_zero_filter(GetExceptionInformation())) { puts("外層的except塊中"); } puts("world"); getchar(); }