VBA 或 VB 中的错误处理是使用语句完成的。虽然这些语句可以毫无问题地与 VB.NET 一起使用,但建议改用语句。 语句比 AND 语句更灵活。On ErrorOn ErrorTryTryOn Error Resume NextOn Error GoTo Label 可以使用语句重写 and 语句的用法。下面演示如何使用 重写语句。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 苏公网安备32011402011833)
GMT+8, 2025-10-29 09:47
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.