在 vb.net 中从一种方法向另一种方法抛出异常

throw exception from one method to another in vb.net

我正在尝试将异常从一种方法抛出到另一种方法,如果发生任何错误,那么主要方法会将其打印在消息框中。我已经在 c# 中完成了,但试图在 vb.net 中编写相同的内容,但是当我提前将它扔给另一个 method.Thanks 寻求帮助时总是出错。

  Public Async Function test() As Task(Of DataSet)

    Try

        Dim ds As New DataSet



        Dim a, b As Integer
        a = "a"
        b = 7
        Dim c = a / b


        Await Task.Run(Function() adapter.Fill(ds))


    Catch ex As Exception
        Throw
    End Try


    Return ds

  End Function

Sub Main()
        Try
            Dim result = test()
            Dim x = result.Result()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Public Async Function test() As Task(Of Integer)
        Try
            Dim c = 1

            Await Test2()

            Await Task.Delay(1000)
            Return 40

        Catch ex As Exception
            Throw
        End Try
    End Function

    Async Function Test2() As Task(Of String)
        Try
            Throw New Exception("mannual")

            Await Task.Delay(1000)

            Return "hello world "
        Catch ex As Exception
            Throw
        End Try
    End Function

您好 Prashant,请检查您的函数调用,已附加更正参考,请查看 CheckInner 异常