473,408 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

exception handling question

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

Nov 17 '05 #1
3 965
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

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2
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

On Thu, 21 Oct 2004 19:36:59 -0500, "Tomas Restrepo \(MVP\)"
<to****@mvps.org> wrote:
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


Nov 17 '05 #3
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

"George Economos" <no****@nospam.com> wrote in message
news:6t********************************@4ax.com...
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

On Thu, 21 Oct 2004 19:36:59 -0500, "Tomas Restrepo \(MVP\)"
<to****@mvps.org> wrote:
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
3
by: dhussong | last post by:
I'm trying to implement a generic exception handling routine that will write information to a text file at the time the exception occurred. I am using the Microsoft Application Block for Exception...
3
by: Larry Herbinaux | last post by:
I have built an asychronous TCP Server that uses the thread pool. I have two levels of exception handling in case the handling of the inner catch block throws an exception. The outer catch block...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
14
by: jehugaleahsa | last post by:
Hello: As an avid reader of C++ books, I know a lot of programmers out there are confronted with the challenge of exception safety. For those who don't know what it is, it is writing code in...
2
by: jayapal | last post by:
Hi , I am using the NEW operator to allocate the memory in many places of my code.But I am not doing any error hadling or exception handling.Can any one suggests me how to do exception handling,...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.