pwasser,
Great info. I suppose this exposed my ignorance. I was not aware that a null pointer exception was not generally caught.
Doing some more research based on your response I found this article:
http://www.gamedev.net/page/resources/_/technical/general-programming/the-visual-c-exception-model-r2488
Among other things, that article states:
"Assuming you want to make sure that catch (...) handles SEH exceptions, the process is relatively simple in MSVC: use the /EHa flag instead of /EHsc for enabling exception handling. For MSVC 7.1 and earlier, this prevents catch blocks from being removed by the optimizer, and is a necessary condition for the catch blocks doing so in MSVC 2005 and later."
I think that explains why my code caught these null pointer situations when complied in VS2003, but not in 2012.
The type of implementation I'm talking about is here:
CString DBShipment::PackageType()
{
try{
return pOrder->pDB->settings.packageTypes[packageType].name;
}catch(...){
}
return CString("");
}
In this function, I could do it more like this:
CString DBShipment::PackageType()
{
if(pOrder){
if(pOrder->pDB){
if(packageType>=0 && packageType<porder->pDB.settings.nPackageTypes){
return pOrder->pDB->settings.packageTypes[packageType].name;
}
}
}
return CString("");
}
However, the solution I used successfully with VC++2003 was less verbose so I took the easy route.
What is still not clear to me. I could leave my code as is and enable the /EHa option to have the code work as I intended. The responses here as well as the article I referenced above say there is a performance downside of this. I wouldn't have expected that to be a big issue for what I am doing here. Any advice on whether to enable this option or switch all my implementations to the second approach I exemplified above?