dcsimg
Results 1 to 3 of 3

Thread: error handler failure

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    44

    error handler failure

    Hi guys! Back with another question. Iv'e got a piece of code that I;m trying to add an error handler to but I'm having problems. Here's the code:
    Code:
    On error goto mistake
            If ListBox1.SelectedItem = "Office 2019" Then
                Dim psi As New ProcessStartInfo() ' Initialize ProcessStartInfo (psi)
                psi.Verb = "runas" ' runas = Run As Administrator
                psi.FileName = ("cmd.exe")
                psi.Arguments = "/c dir > C:\log2.txt"
                Try
                    Process.Start(psi)
                Catch
                    MsgBox("User cancelled the operation", 16, "") ' User pressed No
                End Try
    
    
                Threading.Thread.Sleep(5000)
    
              
                Me.RichTextBox1.LoadFile("C:\log2.txt", RichTextBoxStreamType.PlainText)
                Dim count As String
                count = RichTextBox1.Lines.Count
                TextBox1.Text = RichTextBox1.Lines(14) 'line 14 for office 2019
                Dim str As String = TextBox1.Text
                TextBox2.Text = LTrim(str.Substring(44, str.Length - 44))
                TextBox3.Text = "/c dir:" & TextBox2.Text
                End If
    Exit sub
    Mistake:
    Msgbox”Wrong selection”, Vbokonly, “Error”

    and here's the error I'm getting:

    Error 1 Method cannot contain both a 'Try' statement and an 'On Error' or 'Resume' statement. C:\Users\TheYikes\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb

    I'm totally lost on this one. I'd seriously appreciate any help you guys can offer.

    Thanks in advance!
    TheYikes

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: error handler failure

    Use try catch, and remove the On error. On error is something of vb.6 and not .Net

    p.s

    You can have multiple try catch statements within each other...
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,142

    Re: error handler failure

    Don't use the old "on error" style, as it has various pitfalls that cause complications, the Try-Catch (like you have around Process.Start) is an improved version, so just use that style instead.

    In this case replace this:
    Code:
    On error goto mistake
    with
    Code:
    Try
    ...and replace this:
    Code:
    Exit sub
    Mistake:
    Msgbox”Wrong selection”, Vbokonly, “Error”
    with:
    Code:
    Catch 
      Msgbox”Wrong selection”, Vbokonly, “Error”
    End Try
    However, having a big block of code inside an error handling section (and just a hard-coded message) is a bad idea, because the user (and you) wont know what the actual problem is, so wont be able to do anything about it.

    In this case you have a chunk of code towards the end that loads a file, and there are various issues that can potentially happen within it (such as the file not being there, or being in use, or not containing what your code expects) so it make sense to have error handing around that section - but without an indicator of what the actual problem is, the error message isn't much good.

    You can easily add details of the error to your message like this:
    Code:
    Catch ex as Exception
      Msgbox "The following problem occurred when reading the file:  " & ex.Message, Vbokonly, “Error”
    End Try

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
  •  



Featured


Click Here to Expand Forum to Full Width