0

在下面的代码中,我有嵌套的函数/子

发生的意思是如果有异常然后在代码所在的任何地方停止并将异常添加到全局变量并转到“如果有异常则转到此处”下面的行

由于下面的代码不起作用,我该怎么做?这肯定很容易吗?

下面如果异常发生在 Inner_B() 中,那么它会被 Inner_A() 捕获,但我想在 Inner_B() 中捕获山雀

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim textOutput As String = String.Empty
    Dim strErrorMesssage As String = String.Empty

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Outer()
        'If there was an exception then go here'
    End Sub


    Private Function Outer() As String
        Try
            Inner_A()
        Catch ex As Exception
            strErrorMesssage = ex.Message
            Throw
        End Try
        textOutput &= " Function Outer"
        Return " Function Outer"
    End Function


    Private Function Inner_A() As String
        Try
            Inner_B()
        Catch ex As Exception
            strErrorMesssage = ex.Message & " Inner_A() "
            Throw
        End Try

        textOutput &= " Function Inner_A"
        Return " Function Inner_A"
    End Function

    Private Sub Inner_B()
        Throw New ApplicationException("Exception Occured")
        Try
            Inner_C()
        Catch ex As Exception
            strErrorMesssage = ex.Message & " Inner_B() "
            Throw
        End Try
        textOutput &= " Sub Inner_B"
    End Sub

    Private Function Inner_C() As String
        Try

        Catch ex As Exception
            strErrorMesssage = ex.Message & " Inner_C() "
            Throw
        End Try
        textOutput &= " Function Inner_C"
        Return "Inner_C"
    End Function





End Class
4

1 回答 1

2

我猜你所说的“不工作”是什么意思,但我假设你在调用Outer()?

只需将其包装在 Try Catch 中,但如果您想继续,请不要重新抛出异常。

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Outer()
    Catch ex As Exception
        'do something with it here if you want, or not.
    End Try

    'Do more here.
 End Sub
于 2013-01-18T19:34:35.907 回答