[RESOLVED] on error goto doesn't work twice
hi,
I need something done for multiple textboxes. It works for the first one, but it reaches the second one, the debugging stops because of an error :( Normally it should go on to the next step when this error appears, but this only works for textbox1 :(
here's the code:
Code:
If CheckBox1.Checked = True Then
Dim ary() As String
ary = TextBox1.Text.Split(vbCrLf)
Dim i As Integer
i = 0
If TextBox1.Text <> "" Then
i = 0
TextBox1.Text = TextBox1.Text.Replace("megaupload", "megavideo")
Do
On Error GoTo nextstep2
ary = TextBox1.Text.Split(vbCrLf)
If ary(i).Contains("megavideo.com/?d=") Then
Dim webResponse3 As System.Net.HttpWebResponse = Nothing
Dim webRequest3 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ary(i))
webResponse3 = DirectCast(webRequest3.GetResponse(), System.Net.HttpWebResponse)
Dim srResp As System.IO.StreamReader
srResp = New System.IO.StreamReader(webResponse3.GetResponseStream())
Dim SOMESTRING As String
SOMESTRING = srResp.ReadToEnd
If SOMESTRING.Contains("video is unavailable") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
ElseIf SOMESTRING.Contains("This video has been removed due to infringement") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
Else
For h As Integer = 0 To SOMESTRING.Split("'").Length - 1 Step 1
If (SOMESTRING.Split("'")(h).ToLower().Contains("previewplayer/?v=")) Then
If vlinks = "" Then
vlinks = SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
Else
vlinks = vlinks & vbCrLf & SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
End If
TextBox28.Text = vlinks
End If
Next
End If
Else
If vlinks = "" Then
vlinks = "This line wasn't a MU/MV d-link"
Else
vlinks = vlinks & vbCrLf & "This line wasn't a MU/MV d-link"
TextBox28.Text = vlinks
End If
End If
i = i + 1
Loop
End If
nextstep2:
If TextBox2.Text <> "" Then
i = 0
TextBox2.Text = TextBox2.Text.Replace("megaupload", "megavideo")
Do
On Error GoTo nextstep3
ary = TextBox2.Text.Split(vbCrLf)
If ary(i).Contains("megavideo.com/?d=") Then
Dim webResponse3 As System.Net.HttpWebResponse = Nothing
Dim webRequest3 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ary(i))
webResponse3 = DirectCast(webRequest3.GetResponse(), System.Net.HttpWebResponse)
Dim srResp As System.IO.StreamReader
srResp = New System.IO.StreamReader(webResponse3.GetResponseStream())
Dim SOMESTRING As String
SOMESTRING = srResp.ReadToEnd
If SOMESTRING.Contains("video is unavailable") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
ElseIf SOMESTRING.Contains("This video has been removed due to infringement") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
Else
For h As Integer = 0 To SOMESTRING.Split("'").Length - 1 Step 1
If (SOMESTRING.Split("'")(h).ToLower().Contains("previewplayer/?v=")) Then
If vlinks = "" Then
vlinks = SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
Else
vlinks = vlinks & vbCrLf & SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
End If
TextBox28.Text = vlinks
End If
Next
End If
Else
If vlinks = "" Then
vlinks = "This line wasn't a MU/MV d-link"
Else
vlinks = vlinks & vbCrLf & "This line wasn't a MU/MV d-link"
TextBox28.Text = vlinks
End If
End If
i = i + 1
Loop
End If
End If
nextstep3:
all help is greatly appreciated!
if you know another method to go to the next step when an error occurs (the error is that arr(i) doesn't exists anymore) it's good as well
thanks in advance
LW
Re: on error goto doesn't work twice
Don't use On Error Goto with VB.NET. Ever.
Use Try...Catch...Finally...End Try
Catch and Finally are both optional, but at least one if them has to be present.
Example:
Code:
Dim i As Integer
Try
i = 300 / 0
Catch ex As Exception
'Oh no! Something happened between "Try and Catch"... do this instead:
i = 0
Finally
'Do this after you do everything else.
End Try
Re: on error goto doesn't work twice
I didn't even know On Error GoTo was still supported in .NET (probably shouldn't be)
You can use Try/Catch for each of your textboxes
Re: on error goto doesn't work twice
I agree with Jenner.
Where is the error exactly, and what is the error exactly?
Re: on error goto doesn't work twice
Yes, what is the error? Since you are using IO, error handling is important, but if you can test for the error condition before it happens, the code will run faster, as raising an exception is rather slow.
In general, you could replace your On Error statement with Try, then the NextStep2 would probably be best in the Finally block (add the catch block, but you can leave it empty if you want). You would wrap that NextStep2 in a second Try...Catch block to catch any exceptions in that block.
However, once again, wherever possible, test for the exceptional condition rather than just counting on the error handling.
Re: on error goto doesn't work twice
thanks for the replies. The error is that when arr()=textbox1.text.split(vbcrlf) reaches the last possible line while looping (changes everytime). After this arr(i) doesn't exists so it gives an error. From then it should go to nextstep. So this is not a real error, just an indication that I have looped all the lines of textbox1, and so it should begin again with textbox2.
I already tried the try catch method, but this also didn't work properly, so I found this method, but I'l try again and cross my fingers...
example of error:
textbox1.text =
Code:
megavideo.com/?v=AAAAAAAA
megavideo.com/?v=AAAAAAA6
megavideo.com/?v=AAAAAAA5
megavideo.com/?v=AAAAAAA1
megavideo.com/?v=AAAAAAA2
So the program loops through all the lines using
Code:
arr = TextBox1.text.split(vbcrlf)
and arr(i) where i always changes. But when i reaches 5 in this example (line1 = arr(0)) it gives an error here:
Code:
Dim webRequest3 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ary(i))
as it cannot create the web page ary(5) as ary(5) doesn't exists...
so then it should stop with textbox1 and go on with textbox2
This all works fine, but when he starts with textbox2 the "on error goto" just doesn't work and it the program crashes...
EDIT: I tried with try again, and this time it worked fine! I was probably doing something wrong last time... for the interested people:
Code:
If TextBox1.Text.EndsWith(vbCrLf) Then
TextBox1.Text.Remove(TextBox1.Text.Length - 1, TextBox1.Text.Length)
End If
Dim vlinks As String
If CheckBox1.Checked = True Then
Dim ary() As String
ary = TextBox1.Text.Split(vbCrLf)
Dim i As Integer
i = 0
If TextBox1.Text <> "" Then
i = 0
TextBox1.Text = TextBox1.Text.Replace("megaupload", "megavideo")
Do
Try
ary = TextBox1.Text.Split(vbCrLf)
If ary(i).Contains("megavideo.com/?d=") Then
Dim webResponse3 As System.Net.HttpWebResponse = Nothing
Dim webRequest3 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ary(i))
webResponse3 = DirectCast(webRequest3.GetResponse(), System.Net.HttpWebResponse)
Dim srResp As System.IO.StreamReader
srResp = New System.IO.StreamReader(webResponse3.GetResponseStream())
Dim SOMESTRING As String
SOMESTRING = srResp.ReadToEnd
If SOMESTRING.Contains("video is unavailable") Then
If vlinks = "" Then
vlinks = "Video is not available"
TextBox28.Text = vlinks
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
ElseIf SOMESTRING.Contains("This video has been removed due to infringement") Then
If vlinks = "" Then
vlinks = "Video is not available"
TextBox28.Text = vlinks
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
Else
For h As Integer = 0 To SOMESTRING.Split("'").Length - 1 Step 1
If (SOMESTRING.Split("'")(h).ToLower().Contains("previewplayer/?v=")) Then
If vlinks = "" Then
vlinks = SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
Else
vlinks = vlinks & vbCrLf & SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
End If
TextBox28.Text = vlinks
End If
Next
End If
Else
If vlinks = "" Then
vlinks = "This line wasn't a MU/MV d-link"
TextBox28.Text = vlinks
Else
vlinks = vlinks & vbCrLf & "This line wasn't a MU/MV d-link"
TextBox28.Text = vlinks
End If
End If
Catch ex As Exception
GoTo nextstep2
End Try
i = i + 1
Loop
End If
End If
nextstep2:
vlinks = ""
TextBox1.Text = TextBox28.Text.Replace("&width=", "")
If TextBox2.Text <> "" Then
Dim i As Integer
Dim ary() As String
i = 0
TextBox2.Text = TextBox2.Text.Replace("megaupload", "megavideo")
Do
Try
ary = TextBox2.Text.Split(vbCrLf)
If ary(i).Contains("megavideo.com/?d=") Then
Dim webResponse3 As System.Net.HttpWebResponse = Nothing
Dim webRequest3 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(ary(i))
webResponse3 = DirectCast(webRequest3.GetResponse(), System.Net.HttpWebResponse)
Dim srResp As System.IO.StreamReader
srResp = New System.IO.StreamReader(webResponse3.GetResponseStream())
Dim SOMESTRING As String
SOMESTRING = srResp.ReadToEnd
If SOMESTRING.Contains("video is unavailable") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
ElseIf SOMESTRING.Contains("This video has been removed due to infringement") Then
If vlinks = "" Then
vlinks = "Video is not available"
Else
vlinks = vlinks & vbCrLf & "Video is not available"
TextBox28.Text = vlinks
End If
Else
For h As Integer = 0 To SOMESTRING.Split("'").Length - 1 Step 1
If (SOMESTRING.Split("'")(h).ToLower().Contains("previewplayer/?v=")) Then
If vlinks = "" Then
vlinks = SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
Else
vlinks = vlinks & vbCrLf & SOMESTRING.Split("'")(h).Replace("previewplayer", "http://megavideo.com")
End If
TextBox28.Text = vlinks
End If
Next
End If
Else
If vlinks = "" Then
vlinks = "This line wasn't a MU/MV d-link"
Else
vlinks = vlinks & vbCrLf & "This line wasn't a MU/MV d-link"
TextBox28.Text = vlinks
End If
End If
Catch ex As Exception
GoTo nextstep3
End Try
i = i + 1
Loop
End If
nextstep3:
vlinks = ""
TextBox2.Text = TextBox28.Text.Replace("&width=", "")
was the solution...
Thanks for all your help! You really helped me and much other people that will use the program :D
Re: [RESOLVED] on error goto doesn't work twice
Code:
'arr(0) megavideo.com/?v=AAAAAAAA
'arr(1)megavideo.com/?v=AAAAAAA6
'arr(2)megavideo.com/?v=AAAAAAA5
'arr(3)megavideo.com/?v=AAAAAAA1
'arr(4)megavideo.com/?v=AAAAAAA2
'loop through array without error
For i = 0 To arr.Length - 1
Next
Re: [RESOLVED] on error goto doesn't work twice
Code:
For Each s As String In arr
'Each element of arr becomes "s" in this loop.
Next
Re: [RESOLVED] on error goto doesn't work twice
thanks, that was what I was searching for, but I trying with the .exist and that didn't work...
Thanks again!