Try...Catch block "pauses" while debugging
Hello,
I have a database application with error handling in a separate module.
vb Code:
...
' EDIT A RECORD
With currentCustomer
.Address = txtAddress.Text
.ContactDate = dtpContactDate.Value
'etc...
End With
' SAVE
Try
Db.SubmitChanges()
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I have defined validation code in a partial class:
vb Code:
Partial Public Class Customer
Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
Select Case action
Case ChangeAction.Update
If Me.Address = Nothing Then
Throw New Exception("Fill the Address!")
End If
'etc...
But while debugging (and "Address" field is left empty), the program breaks and I get an exception at the "Throw New Exception" line even if the error should be trapped by the "Try...Catch" block.
At runtime, everything works well and the correct error is displayed.
What is happening?
Re: Try... Catch block stops during debugging
How do you call that OnValidate?
Re: Try... Catch block stops during debugging
OnValidate is called automatically, but I'm afraid your try... catch block does not see it. Can you post your call stack when your exception gets thrown.
Re: Try... Catch block stops during debugging
Unless you call the OnValidate from within some Try..Catch block, I don't see any way how an exception raised from inside it would be caught.
Re: Try... Catch block stops during debugging
The best way to handle that is change:
Code:
Throw New Exception("Fill the Address!")
to
Code:
MsgBox("Fill the Address!")
Or raise some event and handle it gracefully, without throwing an exception.
Re: Try... Catch block stops during debugging
Pradeep, as Cicatrix says, the "OnValidate" event is a "default" one, i learned about it here:
http://weblogs.asp.net/scottgu/archi...-database.aspx
(See the "Custom Entity Object Validation Support" paragraph.
The examples are in C#, but not so difficult to translate).
I am using the same technique: the strange thing is that it does not break the program at runtime,
but only while debugging.
When it breaks, if I continue execution with F5, the program goes on without problems and executes
the "MessageBox.Show(ex.Message)" line correctly.
Re: Try... Catch block stops during debugging
What happens if you add the Overrides keyword?
Code:
Private Overrides Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
Re: Try... Catch block stops during debugging
Syntax error:
"Sub 'OnValidate' cannot be declared 'Overrides' because it does not override a sub in a base class."
Re: Try... Catch block stops during debugging
Onvalidate is a "native" method, see here:
http://blogs.msdn.com/bethmassi/arch...l-classes.aspx
Quote:
In Visual Studio 2008 we can go a step further using Partial methods. Instead of raising and handling private events, partial methods can be used instead as a better performing and cleaner alternative. They are declared by creating a private method with an empty body and decorating it with the Partial keyword. The method may then be "re-implemented" elsewhere within its containing class. If the method is implemented, then the compiler will redirect all calls to the partial method to the implementing method. If the method is not implemented in its containing class, then the compiler silently removes any calls to it from the program.
If we take a look at the generated LINQ to SQL Order class again you can see there is a region called "Extensibility Method Definitions" that contain Partial methods. There will be On...Changed and On...Changing partial methods here for each of the properties on the class.
#Region "Extensibility Method Definitions"
Partial Private Sub OnLoaded()
End Sub
Partial Private Sub OnValidate(action As System.Data.Linq.ChangeAction)
End Sub
Partial Private Sub OnCreated()
End Sub
Partial Private Sub OnCustomerIDChanging(value As Integer)
End Sub
Partial Private Sub OnCustomerIDChanged()
End Sub
Re: Try... Catch block stops during debugging
Here's a good example in VB.NET.
http://blogs.msdn.com/bethmassi/arch...l-classes.aspx
Maybe this helps. :)
Re: Try... Catch block stops during debugging
... is it not the same method I am using?
what I am saying is that it works at runtime (running the compiled EXE), but it stops (or better, "pauses") while debugging.
I have the feeling that it depends on a debugger setting, or something like that....