message loop and exceptions

S

Sankar Nemani

Hi,
I am trying to understand how exceptions behave in a
message loop scenario. I have a try-catch block around a
message loop and one of the event handlers in the message
loop throws an exception. That exception is not caught in
my try-catch block around the message loop. But when
JITDebugging is enabled, the exception is caught in the
same try-catch block. Could someone help me understand
this behavior? When a message loop is started, does it
start a new thread?

To test the regular version I used the following command
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll Test.vb

To test the JITDebugging version I used the following
command with test.exe.config file
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll /debug+ Test.vb

Following are contents of the files I used to test this:
'**********
'Test.vb
'**********
Imports System
Imports System.WIndows.FOrms
imports microsoft.visualbasic


Public Class Form2
Inherits System.Windows.Forms.Form

Friend WithEvents Button1 As
System.Windows.Forms.Button

Sub New()
Me.Button1 = New System.Windows.Forms.Button

Me.Button1.Location = New System.Drawing.Point
(176, 80)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"

Me.ClientSize = New System.Drawing.Size(448, 253)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"

End Sub


Shared Sub main()
Try
Dim frm As New Form2
frm.ShowDialog()
Catch ex As Exception
msgbox("Exception caught in sub main" &
vbcrlf & ex.tostring)
End Try
End Sub

Sub Method1()
Throw New Exception("Test Exception")
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Method1()
End Sub

End Class
'**********
'End Test.vb
'**********

'**********
'test.exe.config
'**********
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
'**********
'end test.exe.config
'**********

TIA
Sankar
 
J

Jay B. Harlow [MVP - Outlook]

Sankar,
Try
Dim frm As New Form2
frm.ShowDialog()
Catch ex As Exception
You are catching any exception that ShowDialog itself throws, not the
exceptions thrown by just an event per se. I would recommend handling the
Application.ThreadException event to catch any exceptions thrown by the
events raised within Form2.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Something like:
Shared Sub main()
AddHandler Application.ThreadException, AddressOf OnThreadException
Dim frm As New Form2
frm.ShowDialog()
End Sub

Private Sub OnThreadException(sender As Object, _
ByVal e As System.Threading.ThreadExceptionEventArgs)
Try
' Log the exception & possible show user the info...
Catch ex As Exception
' Don't want to end the app here! ;-)
End Try
End Sub

Using one (or more) of the above global handlers allows your application to
log the exception in the handler and allows your app to continue processing,
saving you from adding Try/Catch in each of your Form's event handlers for
example.

Hope this helps
Jay
 
R

Rhett Gong [MS]

Hi Senkar,

Thanks for choosing Microsoft MSDN newsgroup!

We would appreciate your patience while we are looking into this issue. We
will update you as soon as we have update for you! Thanks again!

Rhett Gong[MS]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Y

Ying-Shen Yu[MSFT]

Hi Sankar,

Thanks for your post!

I had posted my reply in your another post, if you still have questions on
this issue, please reply
to that thread.
Thank you for your understanding!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

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