469,945 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

MDI Child Errors Crash Application - Unhandled Exceptions

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.
Nov 21 '05 #1
5 4224
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

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #2
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...B;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.

Nov 21 '05 #3
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.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #4
Sam
Thanks Peter, that does the trick. Any reason you know of that stops it
working when debugging?

Sam.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #5
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Shabam | last post: by
12 posts views Thread by Rakesh | last post: by
2 posts views Thread by Marty McDonald | last post: by
1 post views Thread by Jason Smith | last post: by
11 posts views Thread by Roger Vale | last post: by
6 posts views Thread by bjornms | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.