Exception Handling and Page Faults

< BACK  NEXT >
[oR]

A page fault occurs when an application attempts to read or write data from or to a page that does not have memory associated with it, or to a memory address that is illegal. If you try to execute the following code on a desktop PC, you will get an unhandled page fault error box, and your application will terminate.

 char* lpC = 0; *lpC = 'A'; 

The code declares a character pointer and sets it to point at address 0. In most operating systems, including Windows CE, address 0 is protected and cannot be used. The second line attempts to place a character into the address pointed to by lpC, and since the address is protected, a page fault is generated and the application will fail.

Surprisingly, if you attempt to run these two lines of code in Windows CE you will not get a page protection fault the application will continue to execute, although it may not function correctly. This can be a real problem in application development. To ensure that your page faults are correctly reported you will need to use exception handling.

Exception handling allows you to execute code and to trap any errors that would normally be reported by the operating system. Exception handling is a long and complex topic, especially with regard to the rules of how exceptions are handled with C++ object creation and destruction and to nested function calls. To confuse the issue, three types of exception handling exist in Windows programming: MFC (Microsoft Foundation Class), C++ language, and Windows structured exception handling(SEH).

I use Windows structured exception handling (SEH) to trap address and memory exceptions in my applications, and I try to keep it as simple as possible. With SEH the code needed to trap errors is placed in a __try block (that is try with two leading underscores). Errors generated in any function called from this block of code will be trapped. The error-trapping code to be executed in event of an error is placed in an __except block. The EXCEPTION_EXECUTE_HANDLER constant in the __except block indicates that errors will be handled by the block and not passed to other handlers.

 __try {   char* lpC = 0;   *lpC = 'A'; } __except(EXCEPTION_EXECUTE_HANDLER) {   MessageBox(hWnd,     _T("Page Fault Caught in exception handling!"),     szTitle, MB_OK | MB_ICONEXCLAMATION); } 

Now, even in Windows CE, the assignment to a NULL pointer will be trapped and reported. When writing a Windows API function with a message-handling function for a main window, I generally place a __try/__except block around all the code in the message-handling function. Nearly all the code in the application will be called from this function, so any page fault in any function called from the message handler will be trapped.

 LRESULT CALLBACK WndProc(HWND hWnd,       UINT message, WPARAM wParam, LPARAM lParam) {   __try   {     switch (message)     {       case WM_CREATE:         break;       // ... standard message handling code here       default:         return DefWindowProc(hWnd,           message, wParam, lParam);     }   }   __except(EXCEPTION_EXECUTE_HANDLER)   {     MessageBox(hWnd,       _T("Page Fault in exception handling!"),       szTitle, MB_OK | MB_ICONEXCLAMATION);   }   return 0; } 

< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net