我一直在得到這個錯誤,我似乎無法解決try ... catch,我希望你的建議如何解決這個問題。還有更多的代碼,但我相信它與我得到的錯誤有關。 的代碼是這樣的:例外錯誤試圖ping一個定時器
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ip As String
Dim sw As New Stopwatch
ip = "some.ip.com"
Try
If My.Computer.Network.Ping(ip) Then
ping = (sw.ElapsedMilliseconds)
Label1.Text = ping & "ms"
sw.Stop()
MsgBox("You have no connection.")
End If
Catch ex As Exception
MsgBox(ex)
End Try
If ping < 200 Then
Label2.BackColor = Color.Green
Label2.Text = "Good Connection"
ElseIf ping > 200 Then
Label2.BackColor = Color.Orange
Label2.Text = "Medium connection"
ElseIf ping > 400 Then
Label2.BackColor = Color.Red
Label2.Text = "Bad Connection"
ElseIf ping <= 0 Then
Label2.Text = "No Connection"
Label2.BackColor = Color.Brown
End If
的錯誤給了我異常錯誤:
************** Exception Text **************
System.Net.NetworkInformation.PingException: An exception occurred during a Ping request. ---> System.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found
at System.Net.Dns.GetAddrInfo(String name)
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
---內部異常堆棧跟蹤的結尾---在 System.Net.NetworkInformation.Ping.Send (String hostNameOrAddress,Int32 timeout,Byte [] buffer,PingOptions options) at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress,Int32 timeout,Byte [] buffer) at Microsoft.VisualBasic.Devices.Network.Ping(字符串hostNameOrAddress,Int32超時) at Microso ft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress) at C:\ Users \ tonakis2108 \ Documents \ Visual Studio 2013 \ Projects \ something \ something \ Form \ Form2.vb中的Sky_Casino.Form2.Timer1_Tick(Object sender,EventArgs e) :線61 在System.Windows.Forms.Timer.OnTick(EventArgs的) 在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.Callback(IntPtr的的hWnd ,Int32 msg,IntPtr wparam,IntPtr lparam)
我想要我的代碼做的是ping目標,通過sw返回ms,並每5秒做一次。
當我將連接關閉以測試連接到程序時發生的錯誤時,出現此錯誤。誰能幫忙?
它不知道什麼機器是「some.ip.com」。我也沒有。當你拔掉電纜時,你當然也會失去與DNS服務器的連接以及將「some.ip.com」解析爲IP地址的能力。 –
爲什麼你不能「似乎用try ... catch解決」?發生異常時,您可能不想顯示消息框,但可能發現異常被捕獲?你可以在catch塊中設置「ping = -1」嗎? – Mark
@馬克你是什麼意思? 我想在ms中向用戶顯示ping我的意思是說try ... catch不工作,因爲我仍然在計時器上看到異常。 – Tonakis2108