Results 1 to 11 of 11

Thread: Try...Catch block "pauses" while debugging

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Try...Catch block "pauses" while debugging

    Hello,
    I have a database application with error handling in a separate module.

    vb Code:
    1. ...
    2. ' EDIT A RECORD
    3. With currentCustomer
    4.   .Address = txtAddress.Text
    5.   .ContactDate = dtpContactDate.Value
    6.   'etc...
    7. End With
    8.  
    9. ' SAVE
    10. Try
    11.   Db.SubmitChanges()
    12.   Me.Close()
    13. Catch ex As Exception
    14.   MessageBox.Show(ex.Message)
    15. End Try

    I have defined validation code in a partial class:

    vb Code:
    1. Partial Public Class Customer
    2.  
    3. Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
    4.   Select Case action
    5.   Case ChangeAction.Update
    6.     If Me.Address = Nothing Then
    7.       Throw New Exception("Fill the Address!")
    8.     End If
    9. '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.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Try... Catch block stops during debugging

    How do you call that OnValidate?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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.

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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."

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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
  •  



Click Here to Expand Forum to Full Width