try catch question
-
Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }
-
Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }
-
Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }
Yes, your idea is sound. Clearly you will need to define the Error class appropriately and to enable C++ exception handling for your project. Catching exceptions by reference is also occasionaly flaky so if you find the catch part is not working then trying throwing and catching an Error pointer instead.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
Yes, your idea is sound. Clearly you will need to define the Error class appropriately and to enable C++ exception handling for your project. Catching exceptions by reference is also occasionaly flaky so if you find the catch part is not working then trying throwing and catching an Error pointer instead.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
Matthew Faithfull wrote:
Catching exceptions by reference is also occasionaly flaky
I'm not sure what you mean by this. Catching exceptions by reference (usually a
const
reference) is the norm.Steve
-
Matthew Faithfull wrote:
Catching exceptions by reference is also occasionaly flaky
I'm not sure what you mean by this. Catching exceptions by reference (usually a
const
reference) is the norm.Steve
I was recently looking into this because I've been implementing exception handling myself, as opposed to just using it, ( based on porting a VC6 OSS implementation). There's a set of matching rules that decide whether a particular
catch
handler matches a particular thrown exception. The more I looked into how to implement this the more complex I realised it is. Added to which the flags in the underlying Windows (and VC++ generated code ) exception information don't seem adequate to accurately differentiate between for example a reference to a type and a reference to a pointer type. On the basis that I could find no documentation on these rules, maybe they're buried in an appendix of the C++ spec somewhere, I determined to advise extreme caution when throwing anything other than a straight forward pointer. Plain pointers are clearly flagged in the built in data structures and can definitely be caught reliably. I need to do a good deal more investigation, and ideally get hold of the formal rules, to determine if VC++ does indeed generate enough information to fully implement them and whether MSs own exception system has any holes in this area which I strongly suspect it might. I have stuggled in the past to get catch handlers to match with anything other than very simple thrown types and now I think I know why."The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)