vb.net 函数不会在所有代码路径上返回值。使用结果时可能会在运行时发生空引用异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18721925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:01:46  来源:igfitidea点击:

Function doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used

vb.netfunctionreferencenullreturn

提问by HelpASisterOut

I'm getting this error:

我收到此错误:

Function 'getkey' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

函数“getkey”不会在所有代码路径上返回值。使用结果时,可能会在运行时发生空引用异常。

to the following code:

到以下代码:

 Public Function getkey(ByVal id As String)
            Dim cmd As SqlCommand
            Try
                cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@id", id)
                Dim r As SqlDataReader = cmd.ExecuteReader()
                If r.HasRows Then
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            Finally
                ' If Not cn Is Nothing Then cn.Close()
            End Try
        End Function

I tried all possible solutions and they didn't work. Any help would be appreciated.

我尝试了所有可能的解决方案,但没有奏效。任何帮助,将不胜感激。

回答by Douglas Barbin

The Catchblock doesn't return a value. Change it to where it returns a value, like so:

Catch块不返回值。将其更改为返回值的位置,如下所示:

Public Function getkey(ByVal id As String)
        Dim cmd As SqlCommand
        Try
            cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@id", id)
            Dim r As SqlDataReader = cmd.ExecuteReader()
            If r.HasRows Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        Finally
            ' If Not cn Is Nothing Then cn.Close()
        End Try
    End Function

回答by Bas

No value will be returned if an exception is thrown in that try..catch block. You either need to provide a return value in case an exception is thrown (by returning something in either the catch block, or you need to rethrow the exception.

如果在该 try..catch 块中抛出异常,则不会返回任何值。您要么需要提供一个返回值,以防抛出异常(通过在 catch 块中返回某些内容,或者您​​需要重新抛出异常。

Public Function getkey(ByVal id As String)
        Dim cmd As SqlCommand
        Try
            cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@id", id)
            Dim r As SqlDataReader = cmd.ExecuteReader()
            If r.HasRows Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
            ' Either do this:
            ' Return False
            ' or this:
            ' Throw ex
        Finally
            ' If Not cn Is Nothing Then cn.Close()
        End Try
    End Function