Try-catch works with structured exception


G

George2

Hello everyone,


I have tested try-catch works with structured exception, to my
surprise. Previously I think we have to use __try and __except.

Any comments? Here is my test code and I am using Visual Studio 2008.

Code:
#include <iostream>

using namespace std;

int main()
{
	int* address = NULL;

	try{

		(*address) = 1024;
	} catch (...)
	{
		cout << "access violation caught" << endl;
	}
	return 0;
}


thanks in advance,
George
 
Ad

Advertisements

G

Gianni Mariani

George2 said:
Hello everyone,


I have tested try-catch works with structured exception, to my
surprise. Previously I think we have to use __try and __except.

Any comments? Here is my test code and I am using Visual Studio 2008.

This is not portable. It may work with VS 2008. In general, it makes
little sense to catch an access violation, a signal is much more
interesting since you may actually be able to do something interesting -
like mapping memory to the location or whatever.

There are a whole lot of reasons you may get an accvio or segmentation
fault, very few of which you can continue with anything sensible.

In some cases, a divide by zero or other arithmetic fault may be
interesting to catch. I think that gcc has a way of catching those, you
need to recompile your arithmetic code with some compiler flag.
Code:
#include <iostream>

using namespace std;

int main()
{
	int* address = NULL;

	try{

		(*address) = 1024;
	} catch (...)
	{
		cout << "access violation caught" << endl;
	}
	return 0;
}


thanks in advance,
George
 
B

Bo Persson

George2 said:
Hello everyone,


I have tested try-catch works with structured exception, to my
surprise. Previously I think we have to use __try and __except.

Any comments? Here is my test code and I am using Visual Studio
2008.

Code:
#include <iostream>

using namespace std;

int main()
{
int* address = NULL;

try{

(*address) = 1024;
} catch (...)
{
cout << "access violation caught" << endl;
}
return 0;
}

That's a known bug in MS' exception handling. A catch(...) statement
should only pick up things that are thrown.

http://www.thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/


With VS2008 you can presumably select bug or no-bug with the proper
compiler switches.


Bo Persson
 
J

James Kanze

George2 wrote:
I have tested try-catch works with structured exception, to my
surprise. Previously I think we have to use __try and __except.
Any comments? Here is my test code and I am using Visual Studio
2008.
Code:
#include <iostream> 
using namespace std; 
int main()
{
int* address = NULL; 
try{
(*address) = 1024;
} catch (...)
{
cout << "access violation caught" << endl;
}
return 0;
}
That's a known bug in MS' exception handling.

I don't see how it can be considered a bug. I'm pretty sure
they do it intentionally. And of course, it's perfectly
standard conformant.
A catch(...) statement
should only pick up things that are thrown.

In a program which doesn't contain undefined behavior. Once you
encounter undefined behavior, the standard doesn't say what may
happen.

Whether the VC++ behavior is really desirable is another
question---I can't think of any case where a core dump wouldn't
be preferable. But it's certainly better than just stumbling on
randomly. Or locking up the hardware, which is what would
happen on one machine I worked on, many, many years ago.
 
Ad

Advertisements

M

Mike Smith

George2 said:
Hello everyone,


I have tested try-catch works with structured exception, to my
surprise. Previously I think we have to use __try and __except.

Any comments? Here is my test code and I am using Visual Studio 2008.

Actually, that works as far back as Visual Studio 6. The question,
though, is what one would do with such an exception, since once caught
in this fashion you can't (I don't think) get any further information...
 

Top