Results 1 to 9 of 9

Thread: Visual Basic 2010 - Unhandled Exceptions, Please Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Visual Basic 2010 - Unhandled Exceptions, Please Help



    Software\Version:
    Visual Basic 2010 Forms and Applications

    Issue:
    Application shows unhandled exception error message

    Question:

    How do you disable, or auto-click continue on all "Unhandled Exception" error messages?

    Thanks for helping.

  2. #2
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    If you know where the exception is occurring, I think you can put "On Error Resume Next" on the line above, and it will contimue to the next line. Not sure why you would want to ignore exceptions though!

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    First off you should never use On Resume Next, as it is bad to ignore errors. Instead use assertion i.e.

    This is not good if the file does not exist.
    Code:
    IO.File.Delete("Somefile.txt")
    Best to check if the file exists first, this is assertion.
    Code:
    Dim fileName As String = "Somefile.txt"
    . . .
    If IO.File.Exists(fileName) Then
        IO.File.Delete(fileName)
    End If
    For unexpected exceptions you can override the default unhandled exception handler, see attached VS2010 project, compile, run outside of the IDE then inside the IDE. Next look at the error log file created in the app executable folder.

    Bottom line is write tight code, test well and when there is an unexpected error write assertion to trap for this and as a last resort use Try/Catch and never leave the Catch section empty.
    Attached Files Attached Files

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    Thanks.

    On Resume Goto Next worked... lol

    Errors are created by user input, hard to explain but the error is unavoidable,
    and a msg box for it could go bad/several, + handling it may create negative effects/wait/lag
    the error has an ending statement ending on error goto next for all, so no loops.
    Last edited by VBbbq; Jun 20th, 2012 at 08:52 PM.

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    Quote Originally Posted by VBbbq View Post
    Thanks.

    On Resume Goto Next worked... lol

    Errors are created by user input, hard to explain but the error is unavoidable,
    and a msg box for it could go bad/several, + handling it may create negative effects/wait/lag
    the error has an ending statement ending on error goto next for all, so no loops.

    On Resume anything is bad bad bad, this statement is a left over from VB6.

    In regards to errors are unavoidable, this is because you are not using assertion.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    Quote Originally Posted by VBbbq View Post
    hard to explain but the error is unavoidable
    I rather doubt either of those assertions. Maybe if you were to actually show and tell us what you're doing and what exception is being thrown then we could help you handle the situation properly.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    example code with issue:


    If inputidval.Text <> "" And inputid.Checked Then
    On Error Resume Next
    WebBrowser1.Document.GetElementById(inputidval.Text).SetAttribute("Value", inputidnew.Text)
    End If
    End Sub


    when the user puts in a name thats not valid, it would return errors
    if the name they put in wasnt available in webbrowser1.document

    not sure how to resolve it other than the on error resume next thing, it
    worked, is there a better solution?

    im not sure how to write the io.file.exists statement for
    WebBrowser1.Document.GetElementById(inputidval.Text).SetAttribute("Value", inputidnew.Text)

    sounds hard.. :/

    heres a working snippet / example app code


    Dim CodeElements As HtmlElementCollection = WebBrowser1.Document.All
    For Each codeElement As HtmlElement In CodeElements
    Dim code2Element As String = codeElement.GetAttribute("src").ToString
    If code2Element <> "" And codeid.Checked Then
    txtID.Text += "HTML SOURCE: "
    txtID.Text += code2Element
    txtID.Text += vbCrLf
    numElement += 1
    txtElements.Text = numElement
    End If


    if it helps / request for code

    thanks for responding.
    Last edited by VBbbq; Jun 20th, 2012 at 11:50 PM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    Presumably you get a NullReferenceException on this line:
    Code:
    WebBrowser1.Document.GetElementById(inputidval.Text).SetAttribute("Value", inputidnew.Text)
    when the first TextBox contains an ID that doesn't exist. The solution is simple and you have already been provided with it. Simply assign the result of GetElementById to a variable and then test whether that variable is Nothing. If it is then the ID doesn't exist so you don't call SettAttribute. If it's not then you call SetAttribute on the variable. ALWAYS look before you leap, i.e. test the data before you use it and only use it if it's valid. There are very few scenarios where you can't do so.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Re: Visual Basic 2010 - Unhandled Exceptions, Please Help

    Thanks again, I appreciate the help. Everything resolved!

    woot

    /topic finished
    Last edited by VBbbq; Jun 22nd, 2012 at 03:41 PM.

Tags for this Thread

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