VBA 或 VB 中的错误处理是使用语句完成的。虽然语句可以与 VB.NET 一起使用而没有任何问题,但建议改用语句。语句比 theand语句更灵活。On ErrorOn ErrorTryTryOn Error Resume NextOn Error GoTo Label 使用 ofand语句 可以使用语句重写。下面显示了如何使用 重写语句。On Error Resume NextOn Error GoTo LabelTry-CatchOn Error GoTo LabelTry-Catch 出错时 - VBASub ColorEntities() On Error GoTo MyErrorHandler Dim entry As Object For Each entry In ThisDrawing.ModelSpace entry.color = acRed Next entry ' Important! Exit the subroutine before the error handler Exit Sub MyErrorHandler: MsgBox entry.EntityName + " is on a locked layer." + _ " The handle is: " + entry.Handle Resume Next End Sub 尝试捕获 - VB.NET<CommandMethod("ColorEntities")> _ Public Sub ColorEntities() '' Get the current document and database, and start a transaction Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() '' Open the Block table record for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _ OpenMode.ForRead) '' Open the Block table record Model space for read Dim acBlkTblRec As BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _ OpenMode.ForRead) Dim acObjId As ObjectId '' Step through each object in Model space For Each acObjId In acBlkTblRec Try Dim acEnt As Entity acEnt = acTrans.GetObject(acObjId, _ OpenMode.ForWrite) acEnt.ColorIndex = 1 Catch Application.ShowAlertDialog(acObjId.ObjectClass.DxfName & _ " is on a locked layer." & _ " The handle is: " & acObjId.Handle.ToString()) End Try Next acTrans.Commit() End Using End Sub |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )
GMT+8, 2024-5-19 13:53
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.