如何防止我的 VB 应用程序崩溃



我希望你能帮助我解决我的问题。

我收到此错误:

System.Net.WebException:远程服务器返回错误:
(401( 未授权。

当我点击button_2.我想防止应用程序崩溃使用,显示消息框错误或类似的东西。
谁能帮忙?

Private Sub Guna2Button1_Click(sender As Object, e As EventArgs) Handles Guna2Button1.Click
Dim api As String
api = Guna2TextBox1.Text
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("link")
Dim wc As New WebClient
Dim data As String
Dim response As System.Net.HttpWebResponse
Try
response = request.GetResponse()
Catch ex As System.Net.WebException
MsgBox("Bad API")
End Try
MsgBox("Good API")
data = wc.DownloadString("link")
Dim IPV4Regex = New Regex("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
Dim ipstr As String = ("")
Dim total As String = ("")
Dim myJObject = JObject.Parse(data)
For Each match In myJObject("matches")
Dim ip = match("http")("host")
If IPV4Regex.Match(ip).Success Then
ipstr = ip
total = total + ipstr & vbCrLf
Guna2TextBox2.Text = total
Label4.Visible = True
End If
Next
End Sub

试试这个

Dim response As System.Net.HttpWebResponse
Try
response = request.GetResponse()
'Put other lines here...
Catch ex As System.Net.WebException
msgbox("Something bad happened")
End Try

如果 api 错误,为什么你的代码剂量不停止!! 在转到下一步之前,代码必须处理任何错误 至少使用退出子

Try
response = request.GetResponse()
Catch ex As System.Net.WebException
MsgBox("Bad API")
Exit Sub
End Try
MsgBox("Good API")

最新更新