Recently spotted some code on web about aborting transactions when exceptions occur. It is in VB.NET. Here is a cleaned up version.
'...
Dim tr As Transaction = db.TransactionManager.StartTransaction
Try
'...
Dim obj As Object = tr.GetObject(id, OpenMode.ForRead, False, False)
'...
tr.Commit()
Catch ex As ApplicationException
tr.Abort()
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
tr.Dispose()
End Try
The writer only aborts the ‘tr’ Transaction when the ApplicationException occurs. What about other kind of exceptions such as System.AggregateException, System.SystemException and many more that are also parallel to the System.ApplicationException in the System.Exception hierarchy tree?
It seems replacing the ApplicationException with the System.Exception is the fix. However, if she understood why the tr.Dispose() call is there in the Finally block, the tr.Abort() call would have been removed. As analyzed and demonstrated many times before, using the using statement for transactions is a good AutoCAD .NET coding practice in both C# and VB.NET.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Posted by: |