假设您有以下代码:
而不是执行以下操作:
而不是执行以下操作:
Try
'
' Initialize some objects
'
'
' do something that fails
'
'
' Clean up-code that gets not reached because exception
'
Catch e As Exception
'
'Clean up initialized objects
'
Throw e
End Try
我想要做以下操作:
Try
'
' Initialize some objects
'
'
' do something that fails
'
Catch e As Exception
Throw e
Finally
'
'Clean up initialized objects
'
End Try
我的简单问题是:在发生异常的情况下,即使在几行代码之前有一个抛出语句,finally块是否会被执行?
[编辑] 感谢您的快速回答。
我认为第一行会出现NullReference、COM和FileNotFound异常。
好的,我将使用这段代码:
Try
'
' Initialize some objects
'
'
' do something that fails
'
Catch e As Exception ' or just "Catch"??
Throw
Finally
'
'Clean up initialized objects
'
End Try
祝您一切顺利!
Inno
Throw e
而是只写Throw
。否则,你会重置异常堆栈跟踪,从而丢失有关最初引发异常的信息。 - Thomas Levesque