Handling non-user code Exceptions

V

vooose

Is there any way to handle exceptions thrown by .NET classses? During
runtime and at seemingly random times, exceptions similar to the ones
shown at the base of this post are thrown. These errors turn themselves
into serious problems because they usually result in the application
having to exit, even though the user clicks 'Continue'. There doesnt
appear to be much information regarding these issues on forums - does
anyone know a way to handle these and stop the application from exiting?




************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance
of an object.
at System.Windows.Forms.WndProc.Invoke(IntPtr hWnd, Int32 msg, IntPtr
wParam, IntPtr lParam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmUpdateUIState(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
D

Dmitriy Lapshin [C# / .NET MVP]

See MSDN docs on:

Application class, ThreadException event
AppDomain class, UnhandledException event

Also, try to figure out a pattern causing the exception to occur and add
exception handlers to the parts of code involved in the pattern.
Judging by the call stack, it looks like there's a faulty control or some
fancy P/Invoke takes place...
 
V

vooose

Thank you for your reply. I am proceeding to the documentation on those
classes.

You said:
Also, try to figure out a pattern causing the exception >to occur and add
exception handlers to the parts of code involved in the >pattern

Trying very hard to figure out a pattern but its doing its best to be
truly random! :D
I am using

try
{
Application.Run(mainForm);
}
catch(Exception e) { Console.Writeline(e.toString()) }


and as said before, its not caught by this block. I am thinking
somewhere in the non-user code there is a try/catch that brings up the
'Continue' dialog and then promptly exits. If this is the case, then I
assume there is no way to handle this exception? (without fixing the
original problem)

Is there any extra debugging information that can be obtained. Not
having any lines from user-code really makes this tough.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Voose,
I am using

try
{
Application.Run(mainForm);
}
catch(Exception e) { Console.Writeline(e.toString()) }


and as said before, its not caught by this block.

And that's exactly why I refer to Application.ThreadException event (and the
AppDomain.UnhandledException event)!
I vaguely remember there was an exception handling Application Block from
Microsoft...if I am not mistaken and one exists, you might want to check it
out as well.
 
P

Paul F. Williams

We had very similar problems. Our app would crash intermittently.
Attaching with Visual Studio (managed and native), I saw the same stack
traces.

Eventually, I got a trace that made sense. The error message was
something like "Cannot call Dispose when the handle is being created".
I saw there were two threads running. The UI thread was creating a
control, as shown in your stack trace. The second thread was the
garbage collector calling Dispose as part of destroying a control from
a previously opened form. This control had not been Disposed.

It seems there is a potential for a race condition inside the .NET
Framework. If you create a control without adding it to a form, it
does not automatically get Disposed. You have to Dispose of it
manually. If you don't, the garbage collector will attempt to Dispose
of the control. Doing so requires the GC thread to make changes to
global UI data being altered simultaneously on the UI thread.

One example of a control like this is a child form. Say you create the
child form in your parent form's constructor and store it in a local
variable. You must Dispose of this Form yourself. The .NET Framework
does not and cannot do it for you.

For me, disposing of the cached controls in my form's Dispose method
fixed the problem. I hope it fixes your problems, too.
 

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