How to catch an exception for a static object defined in the WndProc () function?

How to catch the exception raised by the static object ain the pseudo-code below, where WndProc()is the standard message processing function in the Win32 API?

class A
{
    public:
    class Exception{};
    A() throw(Exception) { ... }
};

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
    static A a;

    switch( message )
    {
        case WM_CREATE:

        ...

        break;

        ...

        default:

        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}
+3
source share
3 answers

Use a static pointer initialized to NULL and instantiate the class when WM_CREATE is called.

Of course, for any message other than WM_CREATE, if you use an instance, you must double check that the pointer is not NULL - in case you receive messages in an unexpected order.

0
source

-POD- , try and catch ,

+1

WndProc, ++ "" WndProc. ( Windows)

WndProc , WndProc.

,

LRESULT CALLBACK WndProcNoX(....)
{
   try
   {
      static A a;
      ...
   }
   catch(Exception const & x)
   {
      // What do you want to happen?
      // e.g.: add x to a log, or a list of "unhandled" exceptions 
      // that you process e.g. in an Idle or OnTimer handle,
      // or make otherwise accessible.


      return 0; // (*)
   }
}

(*) : 0 " " , , . leng , . !

To protect you from this, you will need to try try / catch for all individual handlers and decide whether to return a return value "in case of error" for each message.

Another option is to pass the message to the default handler in case of an error, which may also be problematic for some messages.

+1
source

All Articles