Re: catching unmanaged exceptions in managed code, not working in release build

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Vasco Lohrenscheit (valohNOSPAM_at_web.de)
Date: 07/30/04


Date: Fri, 30 Jul 2004 15:57:52 +0200

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/default.asp?url=/library/en-us/vcmex/html/vcconBasicConceptsInUsingManagedExceptions.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



Relevant Pages

  • Re: multiple catch exceptions - MS feature request
    ... catch (TimeoutException, InvalidCastException, ..) ... SioComError = true; ... Note that if you have some common code for different types of exceptions, you can simplify the implementation a little in one of two ways: ... -- Refactor the code into a method shared by multiple catch clauses ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: INITIALLY clauses in loop
    ... >> The following actions are exceptions to the linear order of ... > Would it be reasonable to signal a STYLE-WARNING when the exceptions occur? ... clauses, if any, at least according to the grammar in the LOOP ... the INITALLY clauses even though the stepping code corresponding to ...
    (comp.lang.lisp)
  • Re: Real life cost of using exceptions for control flow?
    ... I'm working with an app that uses exceptions for control flow. ... I would not worry about the cost of throwing/catching exception (although is ... because it is polluted by catch clauses that deal with the "normal" logic. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Debugging designer bug
    ... that's not the Designer panel. ... Load any debug project into this instance (for ... exception) so you'll have source code when you enter the debugger. ... Then under Debug choose Exceptions ... ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: System.InvalidOperationException
    ... Thanks Brian - for encouraging me to dig deeper into the exceptions - I love ... I was running directly from the DEBUG folder - so the file wasn't there. ... Compile a debug build and try running it from the bin folder again. ... The type initializer for 'Falcon.PrintConsole.modGlobal' threw an ...
    (microsoft.public.dotnet.framework)