Uncatchable Exception

Z

ZorpiedoMan

Well, I'm still wondering around in the "land of the lost exception"...
It seems that controls that are bound to a class and the class throws an
error in the SET method of the bound member, the exception cannot be
caught. I have wrapped my entire application in all the global
exception handlers I can find, and still to no avail. In fact all the
Try/Catches and Exception event handlers are worthless...

Put on your advanced thinking caps, and help me solve this one, please!

To recreate the problem:
*********************************************************
1. In a new WindowsForm project, add Module1 as follows:
*********************************************************

'----------------------------------------------
'This module is right out of the text book for
'handling unhandled exceptions:
'----------------------------------------------
Module Module1
Public Sub main()
Try
SubMain()
Catch ex As Exception
HandleUnhandledException(ex)
End Try
End Sub
Private Sub SubMain()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException
AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException
Application.Run(New Form1)
End Sub

Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub
Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e
As System.Threading.ThreadExceptionEventArgs)
HandleUnhandledException(e.Exception)
End Sub

Private Sub HandleUnhandledException(ByVal o As Object)
Dim e As Exception = DirectCast(o, Exception)
If Not e Is Nothing Then
Debug.WriteLine("Exception = " + e.GetType().ToString)
Debug.WriteLine("Message = " + e.Message)
Debug.WriteLine("FullText = " + e.ToString())
Else
Debug.WriteLine("Exception = " + o.GetType().ToString)
Debug.WriteLine("FullText = " + o.ToString())
End If
MsgBox(e.Message)
End Sub

End Module



'----------------------------------------------
'Here is the class that the textbox will be
'bound to. Note how it throws an error if you
'try to change the Name property to "Bob"
'----------------------------------------------
Public Class XClass
Private myName As String = String.Empty
Public Property Name() As String
Get
Return myName
End Get
Set(ByVal Value As String)
If Value = "Bob" Then
Throw New Exception("Name Cannot Be Bob")
Else
myName = value
End If
End Set
End Property
End Class



*********************************************************
2. Now add a form to the project with the following code:
*********************************************************


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(24, 80)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(248, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = "TextBox2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 181)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private myXclass As New XClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.DataBindings.Add("Text", myXclass, "Name")
End Sub
End Class


*********************************************************
3. Run the project and set textbox 1 to "Roger" note that you can still
tab to textbox2 or close the form...

4. Now change textbox1 to "Bob" -- you can no longer tab to another
control nor can you close the form... and despite all the error handling
in the program, you can't catch the error!

*********************************************************
SO... WHO CAN FIGURE THIS ONE OUT??? BIG PRIZES, AWARDS, FAME AND GLORY
AWAIT YOU... (OR AT LEAST A BIG THANK YOU FROM ME, IF NOTHING ELSE!)
GOOD LUCK, AND THANK YOU IN ADVANCE!!!
*********************************************************
 
M

Mike Labosh

Interesting. It's because the data binder is ignoring the exception. The
property is being set by something in the framework, not your code.

I bet the Binding class is doing something like this:

Try
oXClass.Name = TextBox1.Text
Catch exc As Exception
'nothing here
End Try

The only way you're going to catch that exception is if the Binding were to
throw it:

Try
oXClass.Name = TextBox1.Text
Catch exc As Exception
Throw exc
End Try

I have never used the new data binding features because I work in a shop
were all we do is giant complex batch processing. Most of my stuff doesn't
even have a UI. But you might explore the features and docs of the data
binding gadgets and see if there's some method to call or property to set
(or another overload of DataBindings.Add()) that makes it operate in another
mode.

--
Peace & happy computing,

Mike Labosh, MCSD

Feed the children!
Save the whales!
Free the mallocs!
 
G

Greg Burns

I was able to duplicate here. Very strange.

Take a look at this thread:

http://tinyurl.com/4ajkn

Seems to indicate that your XClass needs to be implementing some interfaces
in order to play nice with databinding. Not sure myself.

Good luck!
PS (You can send my prize anytime!)
Greg
 
K

Ken Tucker [MVP]

Hi,

System.Exception is thrown when ever an error is thrown. The
exception class inherits from system.applicationexception. They are thrown
when a non-fatal application occurs. Try catching a system.exception
instead.

Try
SubMain()
Catch ex As System.Exception
HandleUnhandledException(ex)
End Try

Ken
-------------------------
Well, I'm still wondering around in the "land of the lost exception"...
It seems that controls that are bound to a class and the class throws an
error in the SET method of the bound member, the exception cannot be
caught. I have wrapped my entire application in all the global
exception handlers I can find, and still to no avail. In fact all the
Try/Catches and Exception event handlers are worthless...

Put on your advanced thinking caps, and help me solve this one, please!

To recreate the problem:
*********************************************************
1. In a new WindowsForm project, add Module1 as follows:
*********************************************************

'----------------------------------------------
'This module is right out of the text book for
'handling unhandled exceptions:
'----------------------------------------------
Module Module1
Public Sub main()
Try
SubMain()
Catch ex As Exception
HandleUnhandledException(ex)
End Try
End Sub
Private Sub SubMain()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException
AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException
Application.Run(New Form1)
End Sub

Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub
Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e
As System.Threading.ThreadExceptionEventArgs)
HandleUnhandledException(e.Exception)
End Sub

Private Sub HandleUnhandledException(ByVal o As Object)
Dim e As Exception = DirectCast(o, Exception)
If Not e Is Nothing Then
Debug.WriteLine("Exception = " + e.GetType().ToString)
Debug.WriteLine("Message = " + e.Message)
Debug.WriteLine("FullText = " + e.ToString())
Else
Debug.WriteLine("Exception = " + o.GetType().ToString)
Debug.WriteLine("FullText = " + o.ToString())
End If
MsgBox(e.Message)
End Sub

End Module



'----------------------------------------------
'Here is the class that the textbox will be
'bound to. Note how it throws an error if you
'try to change the Name property to "Bob"
'----------------------------------------------
Public Class XClass
Private myName As String = String.Empty
Public Property Name() As String
Get
Return myName
End Get
Set(ByVal Value As String)
If Value = "Bob" Then
Throw New Exception("Name Cannot Be Bob")
Else
myName = value
End If
End Set
End Property
End Class



*********************************************************
2. Now add a form to the project with the following code:
*********************************************************


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(24, 80)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(248, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = "TextBox2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 181)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private myXclass As New XClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.DataBindings.Add("Text", myXclass, "Name")
End Sub
End Class


*********************************************************
3. Run the project and set textbox 1 to "Roger" note that you can still
tab to textbox2 or close the form...

4. Now change textbox1 to "Bob" -- you can no longer tab to another
control nor can you close the form... and despite all the error handling
in the program, you can't catch the error!

*********************************************************
SO... WHO CAN FIGURE THIS ONE OUT??? BIG PRIZES, AWARDS, FAME AND GLORY
AWAIT YOU... (OR AT LEAST A BIG THANK YOU FROM ME, IF NOTHING ELSE!)
GOOD LUCK, AND THANK YOU IN ADVANCE!!!
*********************************************************
 
Z

ZorpiedoMan

Three ideas, three misses... but I thank you all for your input. I
agree with the first response, that the reason I am not able to catch
this error is because the binding mechanism is set up to ignore all
errors. Even though I am throwing the error in the SET method, the set
method is being called by the inner code (framework) somewhere that has
an exception handler that ignores all errors. I'd love to hear from
someone at Microsoft on this.

Of course if anyone else has some valuable input or ideas, keep them
coming... the GRAND PRIZE is still up for grabs, and I think People
Magazine is looking to cover this story and might even put your picture
on the cover of next month's magazine if you solve it! ;-)

--Zorpie
 
J

JD

Think validation.

Test1: Take out the throwing of the "Bob" exception, handle the same
textbox's validation event, set the CancelEventArgs Cancel property to true.
Whats the behavior that you get? The same as throwing the "Bob" exception.

Test2: Set the textbox focus causevalidation to false, works like a charm.

JD
 

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