-
Oct 9th, 2014, 10:25 AM
#1
Thread Starter
Junior Member
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
-
Oct 9th, 2014, 10:51 AM
#2
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.
-
Oct 10th, 2014, 04:04 AM
#3
Thread Starter
Junior Member
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
Originally Posted by dday9
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.
-
Oct 10th, 2014, 09:16 AM
#4
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.
-
Oct 10th, 2014, 09:27 AM
#5
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
-
Oct 10th, 2014, 09:50 AM
#6
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
-
Oct 15th, 2014, 04:45 AM
#7
Thread Starter
Junior Member
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 :/ )
-
Oct 15th, 2014, 04:50 AM
#8
Re: Error handling help
Originally Posted by stephen.s13
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.
-
Oct 15th, 2014, 04:56 AM
#9
Thread Starter
Junior Member
Re: Error handling help
Originally Posted by stephen.s13
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
-
Oct 15th, 2014, 04:58 AM
#10
Thread Starter
Junior Member
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
-
Oct 15th, 2014, 08:16 AM
#11
Re: Error handling help
Originally Posted by stephen.s13
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.
Originally Posted by stephen.s13
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.
-
Oct 16th, 2014, 10:02 AM
#12
Thread Starter
Junior Member
Re: Error handling help
Originally Posted by jmcilhinney
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
-
Oct 16th, 2014, 10:25 AM
#13
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.
-
Oct 16th, 2014, 10:36 AM
#14
Thread Starter
Junior Member
Re: Error handling help
Originally Posted by dday9
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
Last edited by stephen.s13; Oct 16th, 2014 at 10:36 AM.
Reason: Bad formatting
-
Oct 16th, 2014, 10:41 AM
#15
Thread Starter
Junior Member
Re: Error handling help
Originally Posted by stephen.s13
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
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
-
Oct 16th, 2014, 10:42 AM
#16
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|