On Error Resger Next时出错

时间:2021-06-16 09:48:35

I can't build exe :

我无法构建exe:

1> ------ Assembly started: project: B, Configuration: Debug Any CPU

B.vb (38.9): error BC36595: The method can not simultaneously contain an "On Error Resume Next" statement and define a variable that is used in the lambda expression or query expression.

B.vb(38.9):错误BC36595:该方法不能同时包含“On Error Resume Next”语句并定义lambda表达式或查询表达式中使用的变量。

Assembling: successful: 0, with errors: 1, no change: 0, skipped: 0

组装:成功:0,有错误:1,无变化:0,跳过:0

From line 37 to 48:

从第37行到第48行:

Sub Read(ByVal ar As IAsyncResult)
    On Error Resume Next
    If TCP.GetStream.DataAvailable And TCP.GetStream.CanRead Then
        Dim tt As New Thread(AddressOf Data) : tt.IsBackground = True : tt.SetApartmentState(ApartmentState.STA) : tt.Start(DirectCast(bf.Deserialize(TCP.GetStream), Byte()))
        TCP.GetStream.Flush() : TCP.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
        Exit Sub
    Else
        GoTo r
    End If 
r:
    Disconnect()
End Sub

I'm trying to use try\catch, but don't know, is that correct? :

我正在尝试使用try \ catch,但不知道,这是正确的吗? :

Sub Read(ByVal ar As IAsyncResult)
    Try
        If TCP.GetStream.DataAvailable And TCP.GetStream.CanRead Then
            Dim tt As New Thread(AddressOf Data) : tt.IsBackground = True : tt.SetApartmentState(ApartmentState.STA) : tt.Start(DirectCast(bf.Deserialize(TCP.GetStream), Byte()))
            TCP.GetStream.Flush() : TCP.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
            Exit Sub
        End If
    Catch
        GoTo r
    Finally
    End Try
r:
    Disconnect()
End Sub

1 个解决方案

#1


0  

The documentation on that error message tells you how to fix it:

该错误消息的文档告诉您如何解决它:

To correct this error

  1. Replace the exception handling code that uses the On Error Goto statement with a Try...Catch statement.
  2. 将使用On Error Goto语句的异常处理代码替换为Try ... Catch语句。

The Try ... Catch will have the following sections:

Try ... Catch将包含以下部分:

  • Try - what should be executed
  • 尝试 - 应该执行什么
  • Catch - What should be executed if an exception is thrown within the Try
  • Catch - 如果在Try中抛出异常应该执行什么
  • Finally - What should be executed whether there is an exception or not.
  • 最后 - 无论是否存在例外,应该执行什么。

I'm trying to use try\catch, but don't know, is that correct?

我正在尝试使用try \ catch,但不知道,这是正确的吗?

It might work, but the Exit Sub and Goto are unnecessary and confuse things. I would instead try:

它可能会起作用,但Exit Sub和Goto是不必要的并且混淆了一些东西。我会尝试:

Sub Read(ByVal ar As IAsyncResult)
    Try
        If TCP.GetStream.DataAvailable And TCP.GetStream.CanRead Then
            Dim tt As New Thread(AddressOf Data) : tt.IsBackground = True : tt.SetApartmentState(ApartmentState.STA) : tt.Start(DirectCast(bf.Deserialize(TCP.GetStream), Byte()))
            TCP.GetStream.Flush() : TCP.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
        End If
    Catch
        Disconnect()
    Finally
    End Try
End Sub

But it seems odd to only call Disconnect() if there is an exception. Should the connection remain open otherwise? How does the caller know if Disconnect has been called or not?

但是如果有异常则只调用Disconnect()似乎很奇怪。连接是否应保持打开?调用者如何知道是否已调用Disconnect?

#1


0  

The documentation on that error message tells you how to fix it:

该错误消息的文档告诉您如何解决它:

To correct this error

  1. Replace the exception handling code that uses the On Error Goto statement with a Try...Catch statement.
  2. 将使用On Error Goto语句的异常处理代码替换为Try ... Catch语句。

The Try ... Catch will have the following sections:

Try ... Catch将包含以下部分:

  • Try - what should be executed
  • 尝试 - 应该执行什么
  • Catch - What should be executed if an exception is thrown within the Try
  • Catch - 如果在Try中抛出异常应该执行什么
  • Finally - What should be executed whether there is an exception or not.
  • 最后 - 无论是否存在例外,应该执行什么。

I'm trying to use try\catch, but don't know, is that correct?

我正在尝试使用try \ catch,但不知道,这是正确的吗?

It might work, but the Exit Sub and Goto are unnecessary and confuse things. I would instead try:

它可能会起作用,但Exit Sub和Goto是不必要的并且混淆了一些东西。我会尝试:

Sub Read(ByVal ar As IAsyncResult)
    Try
        If TCP.GetStream.DataAvailable And TCP.GetStream.CanRead Then
            Dim tt As New Thread(AddressOf Data) : tt.IsBackground = True : tt.SetApartmentState(ApartmentState.STA) : tt.Start(DirectCast(bf.Deserialize(TCP.GetStream), Byte()))
            TCP.GetStream.Flush() : TCP.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
        End If
    Catch
        Disconnect()
    Finally
    End Try
End Sub

But it seems odd to only call Disconnect() if there is an exception. Should the connection remain open otherwise? How does the caller know if Disconnect has been called or not?

但是如果有异常则只调用Disconnect()似乎很奇怪。连接是否应保持打开?调用者如何知道是否已调用Disconnect?