vb.net 关于语句顺序的问题
- VB.NET code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Ports As String() = SerialPort.GetPortNames() Dim Port As String For Each Port In Ports PortNameBox.Items.Add(Port) Next Port SendBox.Text = "123" Label.Text = PortUsing.IsOpen StatusLabel.Text = "串口未连接" StatusLabel.ForeColor = Color.Red PortNameBox.SelectedIndex = 0 AddHandler PortUsing.DataReceived, AddressOf Sp_DataReceived Port_Using() End Sub
为什么有的语句在前那么后面的语句就不执行了?
[解决办法]
当处理前面的语句时出错了,程序就会在出错的地方就马上终止运行了,并抛出异常错误信息,所以后面的语句是执行不了的!
你应该在代码里加上 Try...Catch...Finally 结构来处理异常!