-1

我想在 catch 语句之后运行一段代码,无论发生哪个异常,但仅在发生某些异常时。所以基本上它是一个需要异常才能执行的finally语句。在visual basic中是否有一种简单的方法可以做到这一点?

4

4 回答 4

1

怎么样

Dim isException As Boolean = False
Try
    ....
Catch ex As ApplicationException
    isException = True
    ....
Catch ex As Exception
    isException = True
    ....
Finally
    If (isException)
        ....
    End If
End Try
于 2013-04-12T22:38:24.507 回答
1

我不喜欢它,但是嵌套怎么样Try

Try
    Try
        ....
    Catch ex As ApplicationException
        Throw
    Catch ex As Exception
        Throw
    End Try
Catch
    ' This is your "finally"
End Try
于 2013-04-12T22:44:52.423 回答
0

无论是否引发异常,都会始终调用 If 语句的 finally 块。MSDN

唯一的另一种方法是让另一个方法接受异常作为参数并从异常捕获中调用该方法。

于 2013-04-12T22:02:18.553 回答
0

您可以添加不同的异常块,如下所示:

Try
    ' do operation
' Most specific:
Catch e As ApplicationException
    ' do something only if ApplicationException has occurred
' Least specific:
Catch e As Exception
    Console.WriteLine("{0} Second exception caught.", e);
End Try
于 2013-04-12T22:23:28.520 回答