使用 Try 语句 (.NET)
在 VB.NET 和 C# 中,可以使用语句捕获运行时错误。这句话从字面上为系统设置了一个陷阱。当语句中发生错误时,将绕过系统的默认错误处理,并将执行重定向到其子句。TryTryCatch 该语句有三种形式:Try
Try-Catch 语句当您要响应错误时,将使用该语句。此语句捕获错误,而不是显示默认错误消息并终止应用程序,而是将执行移至语句的子句。Try-CatchCatchTry 该子句可以选择包含接受 Exception 对象的单个参数。Exception 对象包含有关所遇到错误的信息。如果遇到的错误无法解决,则应显示自定义错误消息并正常退出应用程序。Catch Try-Finally 语句当您不想提供特定的错误处理时,将使用该语句。此语句捕获错误,并显示默认错误消息而不终止应用程序。遇到错误时,在默认消息框中单击 Continue 后,执行将从语句移动到其子句。当应用程序仍在开发和调试时,最好使用该语句。Try-FinallyTryFinallyTry-Finally Try-Catch-Finally 语句该语句是 和 语句的组合。此语句捕获错误,而不是显示默认错误消息并终止应用程序,而是将执行移至语句的子句。在子句中执行代码后,执行将移动到该子句,这为应用程序提供了继续执行或正常退出的最后一次机会。Try-Catch-FinallyTry-CatchTry-FinallyCatchTryCatchFinally 在不使用 Try-Catch-Finally 语句的情况下测试错误处理以下示例尝试在 C: 驱动器上打开名为“Drawing123”的文件。如果找不到该文件,则会引发 eFileNotFound 错误。第一个命令不会捕获该方法引发的错误,因此在AutoCAD中启动该命令时会显示默认消息框。第二个命令捕获使用语句引发的错误。ReadDwgFileTry-Catch-Finally VB.NETImports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("NoErrorHandler")> _
Public Sub NoErrorHandler()
'' Create a new database with no document window
Using acDb As Database = New Database(False, True)
'' Read the drawing file named "Drawing123.dwg" on the C: drive.
'' If the "Drawing123.dwg" file does not exist, an eFileNotFound
'' exception is tossed and the program halts.
acDb.ReadDwgFile("c:\Drawing123.dwg", _
System.IO.FileShare.None, False, "")
End Using
'' Message will not be displayed since the exception caused by
'' ReadDwgFile is not handled.
Application.ShowAlertDialog("End of command reached")
End Sub
<CommandMethod("ErrorTryCatchFinally")> _
Public Sub ErrorTryCatchFinally()
'' Create a new database with no document window
Using acDb As Database = New Database(False, True)
Try
'' Read the drawing file named "Drawing123.dwg" on the C: drive.
'' If the "Drawing123.dwg" file does not exist, an eFileNotFound
'' exception is tossed and the catch statement handles the error.
acDb.ReadDwgFile("c:\Drawing123.dwg", _
System.IO.FileShare.None, False, "")
Catch Ex As Autodesk.AutoCAD.Runtime.Exception
Application.ShowAlertDialog("The following exception was caught:" & _
vbLf & Ex.Message)
Finally
'' Message is displayed since the exception caused
'' by ReadDwgFile is handled.
Application.ShowAlertDialog("End of command reached")
End Try
End Using
End Sub
C#using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("NoErrorHandler")]
public void NoErrorHandler()
{
// Create a new database with no document window
using (Database acDb = new Database(false, true))
{
// Read the drawing file named "Drawing123.dwg" on the C: drive.
// If the "Drawing123.dwg" file does not exist, an eFileNotFound
// exception is tossed and the program halts.
acDb.ReadDwgFile("c:\\Drawing123.dwg",
System.IO.FileShare.None, false, "");
}
// Message will not be displayed since the exception caused by
// ReadDwgFile is not handled.
Application.ShowAlertDialog("End of command reached");
}
[CommandMethod("ErrorTryCatchFinally")]
public void ErrorTryCatchFinally()
{
// Create a new database with no document window
using (Database acDb = new Database(false, true))
{
try
{
// Read the drawing file named "Drawing123.dwg" on the C: drive.
// If the "Drawing123.dwg" file does not exist, an eFileNotFound
// exception is tossed and the catch statement handles the error.
acDb.ReadDwgFile("c:\\Drawing123.dwg",
System.IO.FileShare.None, false, "");
}
catch (Autodesk.AutoCAD.Runtime.Exception Ex)
{
Application.ShowAlertDialog("The following exception was caught:\n" +
Ex.Message);
}
finally
{
// Message is displayed since the exception caused
// by ReadDwgFile is handled.
Application.ShowAlertDialog("End of command reached");
}
}
}
父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-29 06:58
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.