CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

使用 Try 语句 (.NET)

2023-1-1 08:55| 发布者: admin| 查看: 351| 评论: 0|来自: AutoCAD

在 VB.NET 和 C# 中,可以使用语句捕获运行时错误。这句话从字面上为系统设置了一个陷阱。当语句中发生错误时,将绕过系统的默认错误处理,并将执行重定向到 itsclause。TryTryCatch

语句有三种形式:Try

  • Try-Catch
  • Try-Finally
  • Try-Catch-Finally

尝试捕获声明

当您要响应错误时,将使用语句。此语句捕获错误,而不是显示默认错误消息并终止应用程序,而是将执行移动到语句的子句。Try-CatchCatchTry

该子句可以选择包含接受异常对象的单个参数。异常对象包含有关遇到的错误的信息。如果遇到的错误无法解决,则应显示自定义错误消息并正常退出应用程序。Catch

最终尝试声明

当您不想提供特定的错误处理时,将使用该语句。此语句捕获错误,并显示默认错误消息,而不终止应用程序。遇到错误时,在默认消息框中单击“继续”后,执行将从语句移动到其子句。该语句最好在应用程序仍在开发和调试时使用。Try-FinallyTryFinallyTry-Finally

尝试-捕获-最终声明

语句是 theand 语句的组合。此语句捕获错误,而不是显示默认错误消息并终止应用程序,而是将执行移动到语句的子句。在子句中执行代码后,执行将移动到子句,这使您的应用程序有最后一次机会继续执行或优雅退出。Try-Catch-FinallyTry-CatchTry-FinallyCatchTryCatchFinally

在没有 Try-Catch-Final 语句的情况下测试错误处理

以下示例尝试在 C: 驱动器上打开名为“Drawing123”的文件。如果未找到该文件,则会引发 eFileNotFound 错误。第一个命令不会捕获方法引发的错误,因此在 AutoCAD 中启动命令时会显示默认消息框。第二个命令捕获使用 the语句引发的错误。ReadDwgFileTry-Catch-Finally

VB.NET

Imports 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");
      }
  }
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-27 09:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部