message loops 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
 
M

MSFT

Hi Sankar,

From my understanding, the problem is the function of try-catch block
depends on if jitDebugging is enabled. However, from your code, the
exception is thrown in the Button's Click event which doesn't contains a
try-catch block, so that no exception will be caught there. Do I
misunderstand something?

Luke
Microsoft Online 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