keyongtech


  keyongtech > cpp

 #1  
01-22-08, 11:13 AM
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;
}
[/Code]


thanks in advance,
George
 #2  
01-22-08, 12:08 PM
Gianni Mariani
George2 wrote:
> 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.
[..]
 #3  
01-22-08, 06:55 PM
Bo Persson
George2 wrote:
[..]
>
> (*address) = 1024;
> } catch (...)
> {
> cout << "access violation caught" << endl;
> }
> return 0;
> }
> [/Code]
>


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/...tion-handling/


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


Bo Persson
 #4  
01-23-08, 12:28 PM
James Kanze
On Jan 22, 7:55 pm, "Bo Persson" <b> wrote:
> 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;
> > }
> > [/Code]


> 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.
 #5  
01-28-08, 03:39 AM
Mike Smith
George2 wrote:
> 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...
Similar Threads
Thread Thread Starter
Handle C++ exception and structured exception together

Hello everyone, I am learning set_se_translator, and there are some good resources about how to translate structured exception into C++ exception, like, [..] 1.

George2
Try-catch works with structured exception

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

George George
Try-catch works with structured exception

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

George
Does Sun's Windows JVM catch Win32 Structured Exceptions?

Hi I have a java program running on a windows platform which makes calls to an external product (dll, .NET) through JNI. In most cases where an exception is generated in...

Barney
Getting the exception code from a structured exception in a catch (...) block

In my program I'm throwing C++ exceptions all around and need to catch structured exceptions and re-throw them as C++ exceptions. I've managed to do it, but it's a bit...

Douglas Peterson

Privacy Policy | All times are GMT. The time now is 12:35 PM.

Merging Information Logo
[Deutschland] [Espaņa] [France] [Italia] [Nederland] [Polska] [United Kingdom]