MDI Child Errors Crash Application - Unhandled Exceptions

S

Sam Loveridge

Hi All.

I'm hoping someone can point me in the direction of a solution to unhandled
exceptions in MDI child forms causing the application to crash.

I've found various articles describing a method using Application.Run(new
MyMainParentForm) to place in the application's Sub Main to allow catching
of unhandled exceptions in the application. The problem I'm facing is that I
want to be able to trap unhandled exceptions at the MDI child form level to
allow me to clean up the child form gracefully without crashing any other
child forms that may be open, and to not then crash the application.

You can test this out by creating an MDI parent form, fire up multiple child
forms and then have a button on one of the child forms that generates an
unhandled exception. You'll find the whole app crashes.

Does anyone have a solution to this problem?

Thanks in advance,

Sam.
 
S

Sara

I don't know I understand ur question correctly or not. but when you
close a MDI child forms by the close button, because, it will dispose
that form which with program you load it in memory and now it unloaded,
then there is no form to show again and therefore It will give u
exception
for not having this problem just add this code to the "Closing" event of
each of child page.

Me.Hide()
e.Cancel = True
I hope this solve your problem
 
P

Peter Huang [MSFT]

Hi

In Windows Form application, we use Application.ThreadException to handle
the Unhandled Exceptions.

Here is a KB article for your reference.
PRB: Unhandled Exceptions in Windows Form Events Are Not Propagated Up Call
Stack (324653)
http://support.microsoft.com/default.aspx?scid=KB;EN-US;324653

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Sam

Hi Sara.

I'm not talking about the standard way of closing a form, but the way an
MDI application will crash if an unhandled exception occurs in an MDI
child.

To test the behaviour:

1) Create a new Visual Basic Windows Application
2) Add a new module called "MainModule" with the following code (you may
need to reformat the code after pasting into the code window) ...

Imports System
Imports System.Windows.Forms

Module MainModule

Sub Main()

Try
' Start the application using a new instance of the MDI parent
form
Application.Run(New MdiParentForm)

Catch ex As Exception
' Display exception
MessageBox.Show(String.Format("The whole application has crashed
with the following error details:{0}{0}{1}", Environment.NewLine,
ex.ToString()), "Terminal Application Exception", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try

End Sub

End Module

3) Add a new form called "MdiParentForm"
4) Set the "IsMdiContainer" property of MdiParentForm to "True"
5) Paste the following code in the Form's Load event:

' Initialise multiple child forms
For count As Integer = 0 To 3
Dim child As New MdiChildForm
child.MdiParent = Me
child.Show()
Next

6) Add another form called "MdiChildForm"
7) Add a button to MdiChildForm form called "CauseExceptionButton"
8) Place the following code in the Click event of CauseExceptionButton:

Throw New Exception("This is a crash test")

9) Set the startup object of the project to be "MainModule"
10) Run the project.

You should see MdiParentForm with multiple child windows. Click on the
button inside one of the child windows to cause an unhandled exception.
Watch the whole app crash.

Can anyone tell me how to isolate the unhandled exception in the child
form that caused it without crashing the whole application?

Cheers,

Sam.
 
S

Sam

Thanks Peter, that does the trick. Any reason you know of that stops it
working when debugging?

Sam.
 
P

Peter Huang [MSFT]

Hi

Per the KB article, this is by design. Also you have many form(parent form,
child form, but there is only one message queue per thread. All the
unhandled exception is "eaten" by Windows Form Application Object, so we
need to use the Application.ThreadException .
CAUSE
The Microsoft .NET Framework wraps the message pump in an exception
handler. The .NET Framework only handles exceptions that reach the message
pump and that the application has not already handled. Therefore, any
unhandled exception that reaches the message pump is not propagated up the
call stack.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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