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
 
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.
 
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top