How to catch an exception thrown by the static object a in 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;
}
Use a static pointer, initialized to NULL, and create the class instance when WM_CREATE is called.
Of course, for any message other than WM_CREATE, if you will be using the instance you should double-check that the pointer is not NULL – just in case you’re getting messages in an unexpected order.