Results 1 to 16 of 16

Thread: Error handling help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Error handling help

    Hi everyone. I have a button that runs 50 different processes but any one of them could occur an error, for example if a file path has changed.
    I need the program to run these commands but tell the user which ones have failed. I have tried the code below but it cuts off after the 1st command regardless of if the next check box is checked

    Code:
    On Error GoTo ErrorHandler1
            If CheckBox1.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.1.50\c$\Transfer\", overwrite:=True)
            Exit Sub
    ErrorHandler1:
            MsgBox("An error occured @Transfer Scarborough -  error  " & Err.Number & ": " & Err.Description & "If Problem persistes, contact the programmer")
    
            On Error GoTo ErrorHandler2
            If CheckBox2.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.2.50\c$\Transfer\", overwrite:=True)
            Exit Sub
    ErrorHandler2:
            MsgBox("An error occured @Transfer York -  error  " & Err.Number & ": " & Err.Description & "If Problem persistes, contact the programmer")
    
            On Error GoTo ErrorHandler3
            If CheckBox3.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.3.50\c$\Transfer\", overwrite:=True)
            Exit Sub
    ErrorHandler3:
            MsgBox("An error occured @Transfer Hessle -  error  " & Err.Number & ": " & Err.Description & "If Problem persistes, contact the programmer")
    Can Anyone help?
    This is replicated 52 times, I.e 52 file copies and 52 error handlers that all need to be dealt with from this one button.

    Also, is it possible for the error message to pop up at the end telling the user which have failed rather than after each transfer attempt as some files we need to send are around 100mbs over a WAN so may need to be left to run over night

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Error handling help

    Take a look at this method:
    Code:
    Private Sub CopyDirectory(ByVal source As String, ByVal destination As String, ByRef ex As Exception)
        Try
            My.Computer.FileSystem.CopyDirectory(source, destination)
        Catch e As Exception
            ex = e
        End Try
    End Sub
    The way that it could be applied is like this:
    Code:
    Dim exceptions As List(Of Exception) = New List(Of Exception)
    Dim ex As Exception = Nothing
    
    For x As Integer = 1 To 52
        Call CopyDirectory("fooDirectory" & x.ToString, "newDirectory" & x.ToString, ex)
    
        If ex IsNot Nothing Then
            exceptions.Add(ex)
            ex = Nothing
        End If
    Next
    
    If exceptions.Count > 0 Then
        For Each e As Exception In exceptions
            Console.WriteLine(e.Message)
        Next
    End If
    Basically what I'm doing is trying to copy the directory and if an exception occurs then I'm adding it to a list of exceptions. Then once I'm finished trying to copy all the directories, I let the user know what exceptions occurred.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Hi,
    That looks like the perfect solution thanks so much.
    Will it be easy enough to make this work with the check checkbox feature? Presumably I can just add that bit on? as below

    Quote Originally Posted by dday9 View Post
    Take a look at this method:
    Code:
    Private Sub CopyDirectory(ByVal source As String, ByVal destination As String, ByRef ex As Exception)
        Try
           if checkbox1.checked = true then My.Computer.FileSystem.CopyDirectory(source, destination)
        Catch e As Exception
            ex = e
        End Try
    End Sub
    The way that it could be applied is like this:
    Code:
    Dim exceptions As List(Of Exception) = New List(Of Exception)
    Dim ex As Exception = Nothing
    
    For x As Integer = 1 To 52
        Call CopyDirectory("fooDirectory" & x.ToString, "newDirectory" & x.ToString, ex)
    
        If ex IsNot Nothing Then
            exceptions.Add(ex)
            ex = Nothing
        End If
    Next
    
    If exceptions.Count > 0 Then
        For Each e As Exception In exceptions
            Console.WriteLine(e.Message)
        Next
    End If
    Basically what I'm doing is trying to copy the directory and if an exception occurs then I'm adding it to a list of exceptions. Then once I'm finished trying to copy all the directories, I let the user know what exceptions occurred.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Error handling help

    That would work, but I would do one further. Because exception handling is notoriously slow, I would place the conditional statement outside of the Try/Catch. Then if the conditional statement is true then you'd follow through with the rest of the code:
    Code:
    If CheckBox1.Checked Then
        Try
            My.Computer.FileSystem.CopyDirectory(source, destination)
        Catch e As Exception
            ex = e
        End Try
    End If
    This is so that you won't have to do the exception handling if there is no need to.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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

    Re: Error handling help

    I'm not sure that I'd bother with that method at all.
    Code:
    For x As Integer = 1 To 52
        Try
            My.Computer.FileSystem.CopyDirectory("fooDirectory" & x.ToString, "newDirectory" & x.ToString)
        Catch ex As Exception
            exceptions.Add(ex)
        End Try
    Next
    
    If exceptions.Count > 0 Then
        For Each ex As Exception In exceptions
            Console.WriteLine(ex.Message)
        Next
    End If

  6. #6
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Error handling help

    jmc's code is missing the checkbox.checked property, otherwise it makes sense. But how about using dday's sub as it is but just call it when needed

    Code:
    If checkBox1.Checked then CopyDirectory("your directory")
    Now, all these solutions are good but what about why was your code doing what it was doing?

    The main issue is that you have an Exit Sub after every copy so it will exit even if the copy is successful.
    OnError will just alter the flow of the code, if you use that (not recommendable spaggetti programming) you should have it return to the original path after handling the error.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Hi all,
    Thanks for the great advise , however I am having trouble getting these to work.
    One option I thought of is to create a log opposed to producing error messages, this way the program will run without stopping and the show the user what worked, what didn't work and what times etc etc

    Code:
     Log.Loginfo.Text &= Environment.NewLine & "------------------------------------------"
            Log.Loginfo.Text &= Environment.NewLine & "Starting New Request" & Now.ToString
    Store001:
            On Error GoTo ErrorHandler001
            If CheckBox1.Checked = False Then GoTo Store002
            If CheckBox1.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.1.50\C$\Transfer\", overwrite:=True)
            Log.Loginfo.Text &= Environment.NewLine & "Files Copied to @Scarborough" & Now.ToString
            CheckBox1.Checked = False
            GoTo Store002
    ErrorHandler001:
            Log.Loginfo.Text &= Environment.NewLine & "Transfer Failure @Scarborough- " & Err.Number & ": " & Err.Description & Now.ToString
            GoTo Store002
    It seems to work so far but if more that one error occurs it still starts debugging in VB rather than just writing to the log. (It does still write to the log :/ )

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

    Re: Error handling help

    Quote Originally Posted by stephen.s13 View Post
    Hi all,
    Thanks for the great advise , however I am having trouble getting these to work.
    One option I thought of is to create a log opposed to producing error messages, this way the program will run without stopping and the show the user what worked, what didn't work and what times etc etc

    Code:
     Log.Loginfo.Text &= Environment.NewLine & "------------------------------------------"
            Log.Loginfo.Text &= Environment.NewLine & "Starting New Request" & Now.ToString
    Store001:
            On Error GoTo ErrorHandler001
            If CheckBox1.Checked = False Then GoTo Store002
            If CheckBox1.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.1.50\C$\Transfer\", overwrite:=True)
            Log.Loginfo.Text &= Environment.NewLine & "Files Copied to @Scarborough" & Now.ToString
            CheckBox1.Checked = False
            GoTo Store002
    ErrorHandler001:
            Log.Loginfo.Text &= Environment.NewLine & "Transfer Failure @Scarborough- " & Err.Number & ": " & Err.Description & Now.ToString
            GoTo Store002
    It seems to work so far but if more that one error occurs it still starts debugging in VB rather than just writing to the log. (It does still write to the log :/ )
    Any code with a GoTo is bad code. Don't use On Error and don't use GoTo. If want error handling then use Try...Catch blocks.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Quote Originally Posted by stephen.s13 View Post
    Hi all,
    Thanks for the great advise , however I am having trouble getting these to work.
    One option I thought of is to create a log opposed to producing error messages, this way the program will run without stopping and the show the user what worked, what didn't work and what times etc etc

    Code:
     Log.Loginfo.Text &= Environment.NewLine & "------------------------------------------"
            Log.Loginfo.Text &= Environment.NewLine & "Starting New Request" & Now.ToString
    Store001:
            On Error GoTo ErrorHandler001
            If CheckBox1.Checked = False Then GoTo Store002
            If CheckBox1.Checked = True Then My.Computer.FileSystem.CopyDirectory( _
        "C:\Transfer\", _
        "\\192.168.1.50\C$\Transfer\", overwrite:=True)
            Log.Loginfo.Text &= Environment.NewLine & "Files Copied to @Scarborough" & Now.ToString
            CheckBox1.Checked = False
            GoTo Store002
    ErrorHandler001:
            Log.Loginfo.Text &= Environment.NewLine & "Transfer Failure @Scarborough- " & Err.Number & ": " & Err.Description & Now.ToString
            GoTo Store002
    It seems to work so far but if more that one error occurs it still starts debugging in VB rather than just writing to the log. (It does still write to the log :/ )
    Biggest issue with this is I will have to write out this code 52+ times, I also have a code to delete the files in the remote locations so I'll have to do that 52+ times also

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Hi, Could you explain a little more please? I.e Where would the "try" go?

    I tried using the on exception catch method however I could not get it to work, I got loads of errors but can't remember what they where now

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

    Re: Error handling help

    Quote Originally Posted by stephen.s13 View Post
    Biggest issue with this is I will have to write out this code 52+ times, I also have a code to delete the files in the remote locations so I'll have to do that 52+ times also
    No, you won't. Have you missed the examples already provided that use loops? That's how you do things multiple times.
    Quote Originally Posted by stephen.s13 View Post
    Hi, Could you explain a little more please? I.e Where would the "try" go?

    I tried using the on exception catch method however I could not get it to work, I got loads of errors but can't remember what they where now
    Someone has already provided an example that uses a Try...Catch block.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Quote Originally Posted by jmcilhinney View Post
    No, you won't. Have you missed the examples already provided that use loops? That's how you do things multiple times.

    Someone has already provided an example that uses a Try...Catch block.
    I'm not sure I have made my intentions clear enough.

    For each check box checked it has to send the file/s from here to a remote server and save any errors (They will be written to a logfile)
    I have tried that code and set the directories, it looks to work and the program hangs like it's working and the nothing. No files moved etc

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Error handling help

    The reason why the program is probably hanging is most likely the way that you're looping. Currently, in my example, I simply looped from 1 to 52. The reason why I looped like that was only for demonstrative purposes, to show you how it would apply. You need to adjust the code to fit your particular need.

    Take a look at this example. It does the same thing, only it loops though each checked CheckBox that sits on the form. This assumes that the CheckBox's text holds the path that you want to copy from and you're copying it to some other location but with the same file name:
    Code:
    Dim exceptions As List(Of Exception) = New List(Of Exception)
    
    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox).Where(Function(c) c.Checked)
        Try
            My.Computer.FileSystem.CopyDirectory("fooDirectory" & cb.Text, "newDirectory" & cb.Text)
        Catch ex As Exception
            exceptions.Add(ex)
        End Try
    Next
    
    If exceptions.Count > 0 Then
        For i As Integer = exceptions.Count - 1 To 0 Step -1
            Console.WriteLine(exceptions.Item(i).Message)
            Console.WriteLine()
        Next
    End If
    That example uses JMcIlhinney's suggestion too by the way.

    The moral of the story is that you cannot expect to copy/paste code and have it work flawlessly. You need to take the principals applied and adjust it to your situation.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Quote Originally Posted by dday9 View Post
    The reason why the program is probably hanging is most likely the way that you're looping. Currently, in my example, I simply looped from 1 to 52. The reason why I looped like that was only for demonstrative purposes, to show you how it would apply. You need to adjust the code to fit your particular need.

    Take a look at this example. It does the same thing, only it loops though each checked CheckBox that sits on the form. This assumes that the CheckBox's text holds the path that you want to copy from and you're copying it to some other location but with the same file name:
    Code:
    Dim exceptions As List(Of Exception) = New List(Of Exception)
    
    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox).Where(Function(c) c.Checked)
        Try
            My.Computer.FileSystem.CopyDirectory("fooDirectory" & cb.Text, "newDirectory" & cb.Text)
        Catch ex As Exception
            exceptions.Add(ex)
        End Try
    Next
    
    If exceptions.Count > 0 Then
        For i As Integer = exceptions.Count - 1 To 0 Step -1
            Console.WriteLine(exceptions.Item(i).Message)
            Console.WriteLine()
        Next
    End If
    That example uses JMcIlhinney's suggestion too by the way.

    The moral of the story is that you cannot expect to copy/paste code and have it work flawlessly. You need to take the principals applied and adjust it to your situation.
    I appreciate I can't simply copy and paste but I couldn't understand how the code worked, It starting to make sense however unfortunately it makes it more awkward. the checkbox.text is the location where it should go but to transfer it I need to use an IP address for example "192.168.1.50\c$". Is that still possible using this loop method?

    Hope fully a picture of the GUI may show my aims
    Name:  program.jpg
Views: 227
Size:  22.5 KB
    Last edited by stephen.s13; Oct 16th, 2014 at 10:36 AM. Reason: Bad formatting

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: Error handling help

    Quote Originally Posted by stephen.s13 View Post
    I appreciate I can't simply copy and paste but I couldn't understand how the code worked, It starting to make sense however unfortunately it makes it more awkward. the checkbox.text is the location where it should go but to transfer it I need to use an IP address for example "192.168.1.50\c$". Is that still possible using this loop method?

    Hope fully a picture of the GUI may show my aims
    Name:  program.jpg
Views: 227
Size:  22.5 KB


    Ohhh, if I change cb.text to cb.name and name the said check boxes as the IP adress that should work? I think after all your help it's eventually sinking in haha. Maybe i'm not cut out for this programming lark

  16. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Error handling help

    I'm not sure how to transfer it where it includes an IP address, but I don't think that you can do that using FileSystem.CopyDirectory. Someone else would need to step in now.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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