473,378 Members | 1,605 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,378 software developers and data experts.

catching unmanaged exceptions in managed code, not working in releasebuild

Hi,

I have a Problem with unmanaged exception. In the debug build it works
fine to catch unmanaged c++ exceptions from other dlls with

//managed code:
try
{
//the form loads unmanaged dlls out of which unmanaged exception //get
thrown
Application::Run(new Form1());
} catch (std::exception& e)
{
//catching work in debud build, in release build a SEHException
//gets thrown
}
But with the release build the same c++ exceptions are not catched but I
get a SEHException from the .net framework. Any idea what can cause this
Problem? I already checked build options and c++ exceptions are enabled
in the release build too, so the problem has to lie somewhere else...

best regards,

Vasco Lohrenscheit
Nov 17 '05 #1
12 6078
"Vasco Lohrenscheit" <va*********@web.de> wrote in message
news:ce**********@online.de...
Hi,

I have a Problem with unmanaged exception. In the debug build it works
fine to catch unmanaged c++ exceptions from other dlls with

//managed code:
try
{
//the form loads unmanaged dlls out of which unmanaged exception //get
thrown
Application::Run(new Form1());
} catch (std::exception& e)
{
//catching work in debud build, in release build a SEHException
//gets thrown
}
But with the release build the same c++ exceptions are not catched but I
get a SEHException from the .net framework. Any idea what can cause this
Problem? I already checked build options and c++ exceptions are enabled
in the release build too, so the problem has to lie somewhere else...


I'm more surprised you get the std exception at all - all our unmanaged
assertions get turned into SEHExceptions anyway. Try working out where the
exception's coming from in the first place, and any differences in the
compiler and linker settings between Debug and Release.

Steve
Nov 17 '05 #2
Steve McLellan wrote:

"Vasco Lohrenscheit" <va*********@web.de> wrote in message
news:ce**********@online.de...
Hi,

I have a Problem with unmanaged exception. In the debug build it works
fine to catch unmanaged c++ exceptions from other dlls with

//managed code:
try
{
//the form loads unmanaged dlls out of which unmanaged exception //get
thrown
Application::Run(new Form1());
} catch (std::exception& e)
{
//catching work in debud build, in release build a SEHException
//gets thrown
}
But with the release build the same c++ exceptions are not catched but I
get a SEHException from the .net framework. Any idea what can cause this
Problem? I already checked build options and c++ exceptions are enabled
in the release build too, so the problem has to lie somewhere else...


I'm more surprised you get the std exception at all - all our unmanaged
assertions get turned into SEHExceptions anyway. Try working out where the
exception's coming from in the first place, and any differences in the
compiler and linker settings between Debug and Release.

Steve


That is what I was going to respond as well, but didn't because I didn't have a
definitive answer.

Sounds like your debug build is behaving incorrectly, and release is operating
as it should.
Nov 17 '05 #3
Julie wrote:

That is what I was going to respond as well, but didn't because I didn't have a
definitive answer.

Sounds like your debug build is behaving incorrectly, and release is operating
as it should.

Does anybody has a definitive answer here?

So unmanaged exception can't be catched in managed code? Well, thats
really bad. Will this change with the standardised c++/CLI binding? Are
there any possibilieties to extract the original unmanaged exception and
its type from the SEHException?

How do you handle unmanaged exceptions in managed code?
best regards,

Vasco Lohrenscheit
Nov 17 '05 #4

"Vasco Lohrenscheit" <va*********@web.de> wrote in message
news:ce**********@online.de...
Julie wrote:

That is what I was going to respond as well, but didn't because I didn't have a definitive answer.

Sounds like your debug build is behaving incorrectly, and release is operating as it should.

Does anybody has a definitive answer here?

So unmanaged exception can't be catched in managed code? Well, thats
really bad. Will this change with the standardised c++/CLI binding? Are
there any possibilieties to extract the original unmanaged exception and
its type from the SEHException?

How do you handle unmanaged exceptions in managed code?


Have a look at SEHException - it allows you to retrieve info related to the
original exception (see MSDN for
System::Runtime::InteropServices::SEHException).

What's unusual is that you were able to catch the std::exception at all. I
don't know how this stands to change in the future; I'm writing production
code so haven't used any of the 2005 beta releases.

Also see this, and have a Google for information about exception handling in
mixed C++.
http://msdn.microsoft.com/library/de...Exceptions.asp
Nov 17 '05 #5
Steve McLellan wrote:
Have a look at SEHException - it allows you to retrieve info related to the
original exception (see MSDN for
System::Runtime::InteropServices::SEHException).

What's unusual is that you were able to catch the std::exception at all. I
don't know how this stands to change in the future; I'm writing production
code so haven't used any of the 2005 beta releases.
Also see this, and have a Google for information about exception handling in
mixed C++.
http://msdn.microsoft.com/library/de...Exceptions.asp


mhh, if I understand the section "Catching Unmanaged C++ Types"
correctly, a catch(const std::exception& e) should work:

" * If an unmanaged C++ type is encountered, the exception is
unwrapped and compared to the type encountered. This comparison allows
an unmanaged C++ type to be caught in the normal way.
* However, if a catch clause of type SEHException or any of its
base classes is examined first, the clause will intercept the exception.
Therefore, you should place all catch clauses that catch unmanaged C++
types first before any catch clauses of managed types.
"

After some testing I've found several problems: The exceptions are only
catched if I debug with F5. Running without debugging neither in the
release nor in the debug build the exceptions are catched. I also tried
to catch the System::Runtime::InteropServices::SEHException and
System::Exception but they aren't catched neither.
But I have enabled the compiler option for exceptions (/EHsc) and
catching exceptions within the unmanaged code works fine.

Any ideas what can cause this strange behaviour? Could it be a problem
that the exceptions are thrown/catched across dll boundaries?

best regards,

Vasco Lohrenscheit
Nov 17 '05 #6
> Steve McLellan wrote:
Have a look at SEHException - it allows you to retrieve info related to the original exception (see MSDN for
System::Runtime::InteropServices::SEHException).
[snip]
mhh, if I understand the section "Catching Unmanaged C++ Types"
correctly, a catch(const std::exception& e) should work:

" * If an unmanaged C++ type is encountered, the exception is
unwrapped and compared to the type encountered. This comparison allows
an unmanaged C++ type to be caught in the normal way.
* However, if a catch clause of type SEHException or any of its
base classes is examined first, the clause will intercept the exception.
Therefore, you should place all catch clauses that catch unmanaged C++
types first before any catch clauses of managed types.
"

After some testing I've found several problems: The exceptions are only
catched if I debug with F5. Running without debugging neither in the
release nor in the debug build the exceptions are catched. I also tried
to catch the System::Runtime::InteropServices::SEHException and
System::Exception but they aren't catched neither.
But I have enabled the compiler option for exceptions (/EHsc) and
catching exceptions within the unmanaged code works fine.

Any ideas what can cause this strange behaviour? Could it be a problem
that the exceptions are thrown/catched across dll boundaries?


Hi,

Maybe. OK, with the following code, I catch an SEHException in both Debug
and Release modes, building into the same assembly. So it may be a DLL
boundary thing, which I can't easily test. It sounds like the debugger may
also be set to break on exceptions, which would give the appearance of
catching something. Try sticking a catch(...) and see if that catches it
(just as a test), otherwise I don't know what to suggest.

ManagedClass::SomeFunction()
{
try
{
// Throws an std::exception
UnmanagedClass::SomeStaticFunction();
}
catch ( System::Runtime::InteropServices::SEHException *ex )
{ }
catch ( std::exception *e )
{ }
catch (...)
{ }
}

UnmanagedClass::SomeStaticFunction()
{
throw std::exception( "test" );
}
Nov 17 '05 #7
Vasco Lohrenscheit wrote:
//managed code:
try
{
//the form loads unmanaged dlls out of which unmanaged exception
//get thrown
Application::Run(new Form1());
} catch (std::exception& e)
{
//catching work in debud build, in release build a SEHException
//gets thrown
}
But with the release build the same c++ exceptions are not catched but I
get a SEHException from the .net framework. Any idea what can cause this
Problem? I already checked build options and c++ exceptions are enabled
in the release build too, so the problem has to lie somewhere else...


I've read the other replies and just wondered: Does Application::Run
have its own catch clause in? If so, I guess its a catch (SEHExecption)
so won't that mean and std::exception will get matched to the
SEHException clause in side Application::Run?

This is how Borland VCL works, each time a message is processed, it is
wrapped in a try/catch block that you can't over-ride.

(Difference is that there, c++ (unmanaged) exceptions aren't caught and
so do propergate back to the main function.

Cheers

Russell
Nov 17 '05 #8

"Russell Hind" <no****@no-where.com> wrote in message
news:Oc**************@TK2MSFTNGP09.phx.gbl...
Vasco Lohrenscheit wrote:
//managed code:
try
{
//the form loads unmanaged dlls out of which unmanaged exception
//get thrown
Application::Run(new Form1());
} catch (std::exception& e)
{
//catching work in debud build, in release build a SEHException
//gets thrown
}
But with the release build the same c++ exceptions are not catched but I
get a SEHException from the .net framework. Any idea what can cause this
Problem? I already checked build options and c++ exceptions are enabled
in the release build too, so the problem has to lie somewhere else...


I've read the other replies and just wondered: Does Application::Run
have its own catch clause in? If so, I guess its a catch (SEHExecption)
so won't that mean and std::exception will get matched to the
SEHException clause in side Application::Run?

This is how Borland VCL works, each time a message is processed, it is
wrapped in a try/catch block that you can't over-ride.

(Difference is that there, c++ (unmanaged) exceptions aren't caught and
so do propergate back to the main function.


There may be something in this, except that if this were the case you'd be
getting an automatic 'unhandled exception' dialog popping up, and this
apparently isn't happening. Still ,in the absence of any more ideas it's
something to look at.

I was under the misguided impression that the exception handler was around
the DLL call. You could try looking at
System::AppDomain::CurrentDomain->UnhandledException and
Application::ThreadException. You can attach exception handlers there for
unhandled exceptions further up the stack.
Steve
Nov 17 '05 #9
>>
I've read the other replies and just wondered: Does Application::Run
have its own catch clause in? If so, I guess its a catch (SEHExecption)
so won't that mean and std::exception will get matched to the
SEHException clause in side Application::Run?

This is how Borland VCL works, each time a message is processed, it is
wrapped in a try/catch block that you can't over-ride.

(Difference is that there, c++ (unmanaged) exceptions aren't caught and
so do propergate back to the main function.

Ahh, thanks Russel. I guess that was the problem. If I catch the
exceptions directly after the call everything works fine.

There may be something in this, except that if this were the case you'd be
getting an automatic 'unhandled exception' dialog popping up, and this
apparently isn't happening. Still ,in the absence of any more ideas it's
something to look at.
Well, I got this unhandled exception dialog :-)

btw: you don't need to catch the SEHException, but can catch the c++
exception directly.

I was under the misguided impression that the exception handler was around
the DLL call. You could try looking at
System::AppDomain::CurrentDomain->UnhandledException and
Application::ThreadException. You can attach exception handlers there for
unhandled exceptions further up the stack.


Isn't it possible to have a global try {} catch(const std::exception& e)
{} somewhere?
In the Application::ThreadException I guess you have to check the type
and then do the exception handling via if() else if()... ?
best regards,

Vasco Lohrenscheit
Nov 17 '05 #10
Steve McLellan wrote:
I was under the misguided impression that the exception handler was around
the DLL call. You could try looking at
System::AppDomain::CurrentDomain->UnhandledException and
Application::ThreadException. You can attach exception handlers there for
unhandled exceptions further up the stack.


In Application::ThreadException I get a SEHException. Is it possible to
extract the original c++ exception from it?
Or any other way to handle c++ exceptions globaly in managed code?

best regards,

Vasco Lohrenscheit
Nov 17 '05 #11
Vasco Lohrenscheit wrote:

In Application::ThreadException I get a SEHException. Is it possible to
extract the original c++ exception from it?
Or any other way to handle c++ exceptions globaly in managed code?


I doubt it. This is the problem with mixed language environments
(again, we have the same problem in C++Builder. Exceptions are normally
pascal. The Run() routine catches pascal exceptions but not C++
exceptions so you have to catch these everywhere yourself or just catch
it in the WinMain and exit the app.

If you could over-ride Application::Run (or a sub part of it) then maybe
it could set up the try catch for SEH then call a 'ProcessMessage'
method which you can over-ride and set up your own try catch for
std::exception and then call inherited ProcessMessage method.

But this would require a change to the CLR I guess.

Cheers

Russell
Nov 17 '05 #12
"Russell Hind" <no****@no-where.com> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Vasco Lohrenscheit wrote:

In Application::ThreadException I get a SEHException. Is it possible to
extract the original c++ exception from it?
Or any other way to handle c++ exceptions globaly in managed code?


I doubt it. This is the problem with mixed language environments
(again, we have the same problem in C++Builder. Exceptions are normally
pascal. The Run() routine catches pascal exceptions but not C++
exceptions so you have to catch these everywhere yourself or just catch
it in the WinMain and exit the app.

If you could over-ride Application::Run (or a sub part of it) then maybe
it could set up the try catch for SEH then call a 'ProcessMessage'
method which you can over-ride and set up your own try catch for
std::exception and then call inherited ProcessMessage method.

But this would require a change to the CLR I guess.


The whole unhandled exception system is a bit weird IMO - I don't know why
Application::ThreadException doesn't follow the same rules as the
UnhandledException handler for the domain. SEHExceptions allow you to
retrieve certain info out of them (have a look at the docs) but AFAIK, not
the underlying type. I'm hoping that MS will look at the way exceptions are
handled, and remove some of the stuff they've put in - it'd be nice if they
just let everything fly through. I guess the problem is that if you let
unmanaged exceptions fly about, some languages (like C#) wouldn't be able to
catch them at all. Exceptions are supposed to have an 'InnerException'
property, but I've not had a look at it. I don't see how it could enclose a
C++ exception though.

Anyway, if you find out any more, let me know, and good luck!

Steve
Nov 17 '05 #13

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

Similar topics

2
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
0
by: Niall | last post by:
I'm attempting to put a C# front end on my unmanaged C++ ray tracer code. It's still in experimental stage, as I have never done anything with exporting types from an unmanaged dll through to a...
4
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage...
2
by: yaron | last post by:
Hi, How do i catch an unmanaged c++ exceptions ? right now i can catch the System.Runtime.InteropServices.SEHException and System.Exception exceptions , but i wan't to catch myException...
3
by: TheLetti | last post by:
Hello out there, is there any possibility for me to catch or avoid crashes caused within unmanaged code? The situation: I'm writing a managed wrapper in MC++, and in there I use some...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
1
by: hcaituiro | last post by:
Can all unmanaged exceptions and others be caught in managed code? Or, you can do nothing to avoid your managed application to crash. Suppouse, you cannot modify the unmanaged code. If you have...
1
by: Bruce | last post by:
I am getting the following exception in the release build of my assembly. {"Attempted to read or write protected memory. This is often an indication that other memory is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.