【发布时间】:2016-04-12 22:56:53
【问题描述】:
在处理对象时,我想检查是否发生异常以选择是否执行 COMMIT。我正在使用针对 SAP 的 BAPI。
如果创建运输单 (1) 失败,我想取消物料单 (2)。 (但这不属于问题)
Dim rfcFunction As IRfcFunction
Try
Using sapConn3 As New SapConnectorV3
' (1) Creation of a Transport-Order
End Using
Catch ex As Exception
Using sapConn3 As New SapConnectorV3
' (2) Cancel MaterialDocument
' (EDIT) The problem can be fixed if I call
' "CommitTransaction" manually at this line. But I want to do
' this automatically on disposing the object
End Using
Throw
End Try
当 Using-Block 结束时,对象被释放,此时我必须知道是否发生了异常。
这个函数不起作用,因为它在两次处理中都声明为 true:
Protected ReadOnly Property ExceptionOccurred() As Boolean
Get
Return Runtime.InteropServices.Marshal.GetExceptionCode <> 0
End Get
End Property
这就是我决定是否提交的方式(部分 Dispose():
Public Sub Dispose() Implements IDisposable.Dispose
If Not ExceptionOccurred Then
CommitTransaction()
End If
【问题讨论】:
标签: .net vb.net exception-handling try-catch dispose