结束函数不 Return 所有代码路径上的值

End Function Doesn't Return a Value on All Code Paths

我在 "End Function" 下有一个绿色波浪线,它提示我说 "Are you missing a return statement?" 当我确实有一个 return 语句时。这是我拥有的:

Private Function TaxCalcFunc(ByVal propVal As Decimal, ByVal perc As Decimal, ByVal rate As Decimal) As Decimal
    Dim AssessedValue As Decimal
    Dim Tax As Decimal
    Try
        AssessedValue = propVal * perc
        Tax = AssessedValue / 100 * rate
        Return Tax
        If rate > 1 Or perc > 1 Then
            Throw New Exception("Tax Greater Than Value. Did you use the decimal Point?")
        End If

    Catch ex As Exception
        MessageBox.Show(0)

    End Try
    'I was unable to get rid of the green squiggle for some reason, says I am missing a return statement
End Function

您在 Catch 语句内部或之后没有 return 语句,因此如果在 Return Tax 代码之前抛出异常,那么您的函数确实不会 return一个值。