exception handling question

G

George Economos

Is the following code correct (managed c++ console app):

// native classes
#pragma unmanaged
class MyException {};

class MyThrow
{
MyThrow()
{
throw MyException();
}
};


// managed main
#pragma managed

void main()
{
try
{
MyThrow m;
}
catch( MyException & e )
{
Console::WriteLine( "Null reference." );
}
}


Using vs.net 2003 on xp sp2 I get a null reference to the exception in
the catch handler. However, If I catch the exception by value
everything works ok. Is the marshalling between native and managed
screwing this up?

Thanks,
George Economos
 
T

Tomas Restrepo \(MVP\)

George,
Is the following code correct (managed c++ console app):

Using vs.net 2003 on xp sp2 I get a null reference to the exception in
the catch handler. However, If I catch the exception by value
everything works ok. Is the marshalling between native and managed
screwing this up?

Seems to work correctly for me here:

#pragma unmanaged
struct MyException
{
int m;
MyException ( int a ) :m(a) { }
};

struct MyThrow
{
MyThrow()
{
throw MyException(3);
}
};


// managed main
#pragma managed

int main()
{
try
{
MyThrow m;
}
catch( MyException & e )
{

Console::WriteLine( e.m );
}
}

tomasr@RADIANT>cl /clr /EHsc /O2 /Og t.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 13.10.3077 for .NET
Framework
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

t.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:t.exe
t.obj

tomasr@RADIANT>t
3
 
G

George Economos

Yeah it works.

The problem seems to be in the debugger. In debug mode the catch
parameter shows as an <undefined> value. I jumped the gun with my
post.

Anyway, are you able to debug/view the catch parameter in debug mode?

Thanks,
George Economos
 
S

Steve McLellan

The debugger has some 'issues' with mixed C++. As far as I know, there's no
plans to fix them for VS 2003 but from the people I've spoken to they are
ironing out issues with 2005. Changing the debugger mode to 'managed only'
often speeds it up / helps it ID variables better / prevents the 'no source
code available' problems. Of course, this is only useful if you don't need
to debug the unmanaged code.

Steve
 

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

Top