【问题标题】:String Value Try Catch in Vb.net字符串值尝试在 Vb.net 中捕获
【发布时间】:2012-12-13 06:22:46
【问题描述】:

我很难理解为什么这总是返回一个空变量

    Private Function checkEnvelopeStatus(aEnvelopeID As String) As String
    Dim lEnvelopeStatusMessage As String

    Try
        Dim lEnvelopeStatus = mDsapi.RequestStatusEx(aEnvelopeID)
        lEnvelopeStatusMessage = "Subject:" & lEnvelopeStatus.Subject & vbCrLf & "Status Code: " & lEnvelopeStatus.Status
    Catch ex As Exception
        MessageBox.Show(ex.StackTrace, ex.Message)
    End Try

    Return lEnvelopeStatusMessage
End Function

但这会返回我想要的消息

    Private Function checkEnvelopeStatus(aEnvelopeID As String) As String
    Dim lEnvelopeStatusMessage As String

    Try
        Dim lEnvelopeStatus = mDsapi.RequestStatusEx(aEnvelopeID)
        aEnvelopeID = "Subject:" & lEnvelopeStatus.Subject & vbCrLf & "Status Code: " & lEnvelopeStatus.Status
    Catch ex As Exception
        MessageBox.Show(ex.StackTrace, ex.Message)
    End Try

    Return aEnvelopeID 
End Function

似乎在我的 TRy 捕获关闭后,字符串的值是函数本地变量时被清除。然而,当我用传入的参数替换它时,我能够保留 Try Catch 外部的字符串?我主要是 C#/C++ 开发人员,所以这对我来说是令人困惑的行为。谁能解释为什么会发生这种情况?

这里是上面代码的更干净的版本

Public Function foo(a As String) As String
    Dim b As String
    Try
        b = "banana:"
    Catch ex As Exception

    End Try

    Return b
End Function

它表现出相同的行为。

【问题讨论】:

    标签: c# vb.net declaration definition declare


    【解决方案1】:

    我用 C# 写了同样的代码

    public string foo(string a)
    {
        String b;
        try
        {
            b = "banana";
        }
        catch
        {
    
        }
    
        return b;
    }
    

    }

    它似乎表现出相同的行为。我被带回来了。多想一想,还是有道理的。我只是在声明变量并没有真正将它定义为任何东西。所以把上面的例子换成这样的:

            public string foo(string a)
        {
            String b = "";
            try
            {
                b = "banana";
            }
            catch
            {
    
            }
    
            return b;
        }
    }
    

    解决了我所有的问题。希望这可以帮助将来的人。你知道的越多……

    【讨论】:

      【解决方案2】:

      在 C# 中,您无法在执行流程中编译存在路径的方法代码,这可能导致在未初始化返回值的情况下退出方法。使用未分配的变量会出现编译器错误。

      在 VB.NET 中,如果您更改了选项,则可以避免该错误 “在分配之前使用变量”,从您的项目选项的构建页面中的警告错误。 (我强烈建议这样做)

      (不确定选项的确切名称,因为我使用的是本地化版本的 Visual Studio)

      【讨论】:

        【解决方案3】:

        那个代码不应该编译,我试过了,它给了我一个编译器错误“错误3使用未分配的局部变量'b'”

        你的编译器有问题,你用的是什么?

            static public string foo(string a)
            {
                String b;
                try
                {
                    b = "banana";
                }
                catch
                {
        
                }
        
                return b;
            }
        

        【讨论】:

          【解决方案4】:

          在你重新抛出 excaption 之前它不会编译。考虑以下:

          public String foo(String a) {
              String b;
          
              try {
                  b="banana:";
              }
              catch {
                  throw;
              }
          
              return b;
          }
          

          这段代码可以编译,因为如果出现异常,它会重新抛出。 编译器预测逻辑问题,例如无法访问的代码(尽管无法访问的代码不是错误)。 VB.net 中的等价:

          Public Function foo(a As String) As String
              Dim b As String
          
              Try
                  b = "banana:"
              Catch ex As Exception
                  Throw ex
              End Try
          
              Return b
          End Function
          

          顺便说一句,捕获任何异常被认为是一种不好的编码习惯。

          【讨论】:

            猜你喜欢
            • 2018-01-31
            • 2020-06-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-02
            相关资源
            最近更新 更多