Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've recently upgraded my code to compile in Visual Studio 2012 C++ from 2003. I know this may not be good form, but I tend to use a lot of try{}catch(...){} blocks rather than checking for NULL objects that could cause access violations. I didn't get any compile time errors, but in release mode rather than jumping to the catch(...){} block when such an error is encountered the application just crashes with the windows MyApp was forced to close window. Is this no longer supported? When I read the microsoft documentation:
http://msdn.microsoft.com/en-us/library/6dekhbbc(v=vs.110).aspx[^]

It doesn't give any indication that this syntax is no longer supported. Obviously I could update my code to get rid of this, but I'm bound to miss some instances and I'd rather avoid it if possible.

Thanks,
David
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-14 22:52pm    
Not enough information.

In certain sense, "a lot of try-catch" can be even worse than many checks for null. It means abuse and not understanding exceptions, which actually propagate across the stack and hence can be handled very rarely, only in some strategically selected points.

VC 2003 is just too old. Chances are, newer C++ works with your code correctly, and old C++ does not...

—SA

Null pointer exceptions are not a part of c++.

You can however catch exceptions such as division by zero, access violation, etc. in Visual Studio using try -> catch (...) block by enabling /EHa option in project settings.

Modify Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions".

Refer to:http://msdn.microsoft.com/en-us/library/1deeycx5%28v=vs.80%29.aspx[^]
This can be demonstrated using this code:
C++
#include <conio.h>
#include <iostream>

using namespace std;

void main(void)
{
  try {
        int* ptr = NULL;
        *ptr = 1;
    } catch (...) {
        cout << "Null pointer exception caught." << endl;
    }

	getch();
}


With SEH enabled an exception is caught otherwise not. There is a performance penalty to this approach.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 1-Apr-14 22:59pm    
Actually, right, good point, a 5.
—SA
[no name] 1-Apr-14 23:15pm    
Thanks
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:

C++
CString DBShipment::PackageType()
{
	try{
		return pOrder->pDB->settings.packageTypes[packageType].name;
	}catch(...){
	}

	return CString("");
}


In this function, I could do it more like this:
C++
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?
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900