Zorpy,
Using the Application.Thr eadException event you can trap the exception &
show it to the user or log it. Something like:
Public Shared Sub Main()
AddHandler Application.Thr eadException, AddressOf
Application_Thr eadException
Application.Ena bleVisualStyles ()
Application.DoE vents()
Application.Run (New ZorpiedoMan)
End Sub
Private Shared Sub Application_Thr eadException(By Val sender As Object,
ByVal e As System.Threadin g.ThreadExcepti onEventArgs)
MessageBox.Show (e.Exception.To String(), e.Exception.Mes sage)
End Sub
In the above case I might consider having my validation code throw a
ValidationExcep tion, allowing Application_Thr eadException to decide to show
it to the user or simply log it.
Where ValidationExcep tion is a new class that inherits from System.Exceptio n
with information on what validation failed...
Private Shared Sub Application_Thr eadException(By Val sender As Object,
ByVal e As System.Threadin g.ThreadExcepti onEventArgs)
If TypeOf e.Exception is ValidationExcep tion Then
' handle validation exceptions
Else
' handle unhandled exceptions
End
MessageBox.Show (e.Exception.To String(), e.Exception.Mes sage)
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.Http Application.Err or event
Normally placed in your Global.asax file.
For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use AddHandler in your Sub Main.
For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion 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/is...T/default.aspx
For example: In my Windows Forms apps I would have a handler attached to the
Application.Thr eadException 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.Thr eadException handler will catch all
uncaught exceptions from any form/control event handlers.
Hope this helps
Jay
"ZorpiedoMa n" <No********@Bea tles.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
TextBox1.DataBi ndings.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!