|
-
Feb 8th, 2010, 06:44 AM
#1
Thread Starter
Lively Member
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?
Last edited by axplains; Feb 8th, 2010 at 10:10 AM.
-
Feb 8th, 2010, 07:06 AM
#2
Re: Try... Catch block stops during debugging
How do you call that OnValidate?
-
Feb 8th, 2010, 07:18 AM
#3
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.
-
Feb 8th, 2010, 07:42 AM
#4
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.
-
Feb 8th, 2010, 07:44 AM
#5
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.
-
Feb 8th, 2010, 08:08 AM
#6
Thread Starter
Lively Member
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.
Last edited by axplains; Feb 8th, 2010 at 08:13 AM.
-
Feb 8th, 2010, 08:52 AM
#7
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)
-
Feb 8th, 2010, 09:08 AM
#8
Thread Starter
Lively Member
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."
-
Feb 8th, 2010, 09:26 AM
#9
Thread Starter
Lively Member
Re: Try... Catch block stops during debugging
Onvalidate is a "native" method, see here:
http://blogs.msdn.com/bethmassi/arch...l-classes.aspx
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
-
Feb 8th, 2010, 09:31 AM
#10
Re: Try... Catch block stops during debugging
-
Feb 8th, 2010, 09:55 AM
#11
Thread Starter
Lively Member
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....
Last edited by axplains; Feb 8th, 2010 at 10:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|