BUG: An exception does not propagate correctly to the calling function in a Windows Forms applicatio

  • Thread starter Thread starter RalphTheExpert
  • Start date Start date
R

RalphTheExpert

This thread is a continuation of a thread with the Subject "Unhandled
exception - Different under debugger and non-debugger".
(http://www.dotnetnewsgroups.com/newsgroupthread.asp?ID=186902)



Oleg and Carl:

Carl wrote: "I would think the only fully sanitary, guaranteed to work
way would be to either terminate the program from inside the handler, or
intern the exception in an object and pass (a pointer to) that object
through a WM_APP window message where your message loop can unwrap it
and re-throw or otherwise handle the exception."

I’m pretty good when it comes to MFC and WTL. I am just learning how to
use the .Net environment and, frankly, there is a whole new gestalt here
that I’m missing. I am clueless as to whether a message pump even exists
in this environment. I’m guessing that there probably is and it has
been buried deep so that mere mortals don’t have to mess with it.



So, Oleg, my question to you is, “Does what Carl suggests solve the
problem?” If it does, what’s the right way to do it?

Fleshing out Carl’s remark, I assume that the proper way to do what he
says is to do a RegisterWindowMessage to get a unique message number and
to post a message. My problem is that I am clueless as to how to code a
handler to process the message.


Another question to you, Oleg, is that the article you cited
(http://support.microsoft.com/default.aspx?scid=kb;en-us;836674) seems
to be talking about this in terms of SEH rather than C++ exceptions.



Another point of confusion that I have is that the article you cited
says “In Visual Studio .NET 2003, click Application Configuration File
under Template, and then click Open”. Well, I’m running 2003 and there
is no Application Configuration File under Template. There’s a resource
template icon but no “XML file” under it. Sigh.

I followed the procedure under 2002 (even though I’m running 2003). I
followed those instructions and, as far as I can tell, there are no
behavioral differences. Sigh.

I haven’t tried MS’s “Another workaround for this problem is to use the
Application.OnThreadException handler” because I don’t understand what
they’re trying to accomplish.
 
So, Oleg, my question to you is, "Does what Carl suggests solve the
problem?" If it does, what's the right way to do it?

Yes, it does, but only for native C++ exceptions, while your application
seems to use managed exceptions. Managed exceptions can be thrown/caught
across message pumps.

(In case of native exceptions, my solution would be to catch everything that
I know should be caught before it can potentially cross CRT boundaries
(that is, do not allow native C++ exceptions that are used by my code
to leave the message handlers). I would leave everything else to the default
just-in-time debugging mechanism).

Another question to you, Oleg, is that the article you cited
(http://support.microsoft.com/default.aspx?scid=kb;en-us;836674) seems
to be talking about this in terms of SEH rather than C++ exceptions.

It is talking about managed exceptions. It applies to System::Exception
and types derived from it. Btw, how is your exception class (MyException) defined?
Another point of confusion that I have is that the article you cited
says "In Visual Studio .NET 2003, click Application Configuration File
under Template, and then click Open". Well, I'm running 2003 and there
is no Application Configuration File under Template. There's a resource
template icon but no "XML file" under it. Sigh.

Create the file manually, name it yourapp.exe.config and place it into the same
directory with yourapp.exe (substitute the real name, of course :)
The file should contain the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

This should change the behavior of your application when running without debugger
(your handler will be able to catch the exception).

Oleg
 
Oleg:

[snip]
It is talking about managed exceptions. It applies to System::Exception
and types derived from it. Btw, how is your exception class (MyException) defined?


/* MyException.h */

#pragma once

#using <mscorlib.dll>


public __gc
class MyException: public System::Exception
{
public:
/*
Constructors, private methods, and other stuff.

Oleg, do you need/want the full class?
*/
}; /* class MyException: System::Exception */




Create the file manually, name it yourapp.exe.config and place it into the same
directory with yourapp.exe (substitute the real name, of course :)
The file should contain the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

This should change the behavior of your application when running without debugger
(your handler will be able to catch the exception).

Thanks!

Is it my imagination, but does it take a very long time for the
executable to load the first time it is loaded when I put in
myApp.exe.config?

Ralph
 
Back
Top