The Land Of The Lost Exception

Z

ZorpiedoMan

I just ran across an interesting phenon...

If you bind a control, say a textbox, to a class.property, and in
changing the value of the textbox an error is thrown in the
class.property, you get stuck in the textbox and cannot leave:

Public Class XClass
Private myName as String
Public Property Name as String
Get
Return myName
End Get
Set (Value as String)
if Value = "Bob" Then
Throw New Exception("Name Cannot Be Bob")
Else
myName = value
End If
End Property

---------------- On the form, you have a textbox (textbox1)
-------------

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 New Binding("Text", myXClass, "Name")

End Sub

-------------------------------

So, if you change the textbox from "Mary" to "Bob", the exception seems
to get thrown, but I can't figure out how to catch it.

Any ideas?

--Zorpy


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

Jay B. Harlow [MVP - Outlook]

Zorpy,
Using the Application.ThreadException event you can trap the exception &
show it to the user or log it. Something like:

Public Shared Sub Main()
AddHandler Application.ThreadException, AddressOf
Application_ThreadException
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New ZorpiedoMan)
End Sub

Private Shared Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)
MessageBox.Show(e.Exception.ToString(), e.Exception.Message)
End Sub

In the above case I might consider having my validation code throw a
ValidationException, allowing Application_ThreadException to decide to show
it to the user or simply log it.

Where ValidationException is a new class that inherits from System.Exception
with information on what validation failed...

Private Shared Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)
If TypeOf e.Exception is ValidationException Then
' handle validation exceptions
Else
' handle unhandled exceptions
End
MessageBox.Show(e.Exception.ToString(), e.Exception.Message)
End Sub

Note I normally do my logging in a global exception handler.

Also I use try/finally more then I use try/catch. I only use try/catch when
there is something specific that I need to do with the exception, otherwise
I let my global exception handlers handle the exception.


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.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
 
Z

ZorpiedoMan

Jay -

Many, MANY thanks for this thoughful and complete answer. Not only have
you given me a good response to my question, you have given me some new
insights as to error handling in general. I had error handling in VB6
down pat, but had not yet had the time/energy/knowledge to get a
comfortable, standard way of handling errors yet in .NET. This will
help a lot in that endeavor. Thanks again.

--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Z

ZorpiedoMan

Jay -

I'm only now getting around to trying your suggestions and reading
the article you suggested. (Finally!)

Guess What? Neither your sample, or any of the samples in the
article catch the exception either!

Now What?

--Zorpie
 
J

Jay B. Harlow [MVP - Outlook]

Zorpie,
If you are referring to the info I gave over a month ago, you will need to
include more information on what specifically is not working, as the example
and articles I gave work as advertised. Which of course may not be exactly
what you are expecting.

Hope this helps
Jay
 

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