📄 frmcommonexceptions.vb
字号:
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Net
Imports System.Diagnostics
Imports System.Net.Sockets
Namespace CodeForChapter4cs
Public Partial Class frmCommonExceptions
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
label1.Text = ".."
Try
' Create a web request for a site.
Dim wr As HttpWebRequest = CType(WebRequest.Create(textBox1.Text), HttpWebRequest)
' Get the associated response for the above request.
Dim rsp As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
label1.Text = rsp.StatusCode.ToString()
rsp.Close()
Catch ex As WebException
Debug.WriteLine("This program is expected to throw WebException on successful run." & Constants.vbLf + Constants.vbLf & "Exception Message ")
MessageBox.Show(ex.Message)
If ex.Status = WebExceptionStatus.ProtocolError Then
If Not ex.Response Is Nothing Then
'label1.Text = ((HttpWebResponse)ex.Response).StatusCode.ToString();
Debug.WriteLine("Response is null")
Return
End If
Debug.WriteLine("Status Code : " & (CType(ex.Response, HttpWebResponse)).StatusCode)
Debug.WriteLine("Status Description : " & (CType(ex.Response, HttpWebResponse)).StatusDescription)
Else
Debug.WriteLine(ex.Status.ToString())
End If
If ex.InnerException IsNot Nothing Then
Debug.WriteLine(ex.InnerException.ToString())
Else
Debug.WriteLine("was null")
End If
Catch ex2 As Exception
Debug.WriteLine(ex2.Message)
End Try
End Sub
Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem2.Click
Try
Dim ipAddress As IPAddress = Me.GetIpAddressSomehow()
If ipAddress Is Nothing Then
MessageBox.Show("Sorry could not get ip address.")
Return
End If
Dim port As Integer
Try
port = Convert.ToInt32(textBox1.Text)
Catch e1 As FormatException
MessageBox.Show("Please provide a valid port number.")
Return
End Try
Dim ipe As IPEndPoint = New IPEndPoint(ipAddress, port)
Try
Me.ConnectToThis(ipe)
Catch ex As CantConnectException
LogException(ex)
MessageBox.Show("Sorry, could not connect")
End Try
Catch ex As Exception
LogException(ex)
MessageBox.Show("Sorry, an unexpected error has occured. Please shutdown this application.")
ExitApplication()
End Try
End Sub
Private Sub ExitApplication()
Throw New Exception("The method or operation is not implemented.")
End Sub
Private Function GetIpAddressSomehow() As IPAddress
'throw new Exception("The method or operation is not implemented.");
End Function
Private Sub ConnectToThis(ByVal ep As IPEndPoint)
End Sub
Private Function ConnectToThis2(ByVal ep As IPEndPoint) As Boolean
Dim s As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
s.Connect(ep)
Return True
Catch e1 As SocketException
' translate exception to return result
Return False
Catch e2 As ArgumentNullException
' IPEndPoint passed in is null. Pass it up, caller's fault.
Throw
Catch ex As Exception
LogException(ex) 'ouw own logging method
Throw
End Try
End Function
Private Sub LogException(ByVal ex As Exception)
'throw new Exception("The method or operation is not implemented.");
End Sub
Public Event CantConnect As EventHandler
Private Sub UpdateTimer(ByRef tmr As System.Threading.Timer)
Try
tmr.Change(2000, 10000)
Catch e1 As ObjectDisposedException
'tmr = new System.Threading.Timer(...);
tmr.Change(2000, 10000)
End Try
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -