使用Try语句
 
 

In VB.NET and C#, runtime errors can be trapped using a Try statement. This statement literally sets a trap for the system. When an error occurs in a Try statement, the default error handling for the system is bypassed and execution is redirected to its Catch clause.

在 VB.NET 和 C# 中,利用 Try 语句可以捕捉运行时错误。这个语句会逐个设置陷阱。当错误在 Try 语句中出现时,默认的错误处理程序将被绕过并重定向到 Catch 子句继续执行。

The Try statement has three forms:

Try 语句有三种形式:

Try-Catch 语句

The Try-Catch statement is used when you want to respond to an error. This statement traps the error and instead of displaying a default error message and terminating the application, execution is moved to the Catch clause of the Try statement.

当你想响应一个错误时使用 Try-Catch 语句。这个语句捕捉错误并代替显示一个默认的错误信息,然后终止应用程序,最后会将当前运行行移动到 Try 语句的 Catch 子句中去。

The Catch clause can optional contain a single parameter which accepts an Exception object. The Exception object contains information about the error encountered. If the error that is encountered cannot be resolved, you should display a custom error message and exit the application gracefully.

Catch 子句包含一个可选的接收 Exception 对象的参数。Exception 对象关于所遇到的错误的详细信息。如果遇到的错误不能被确定,就应该显示一个自定义的错误信息并退出应用程序。

For information on the Exception object, see Use the Exception Object.

关于 Exception 对象的详细信息,请参见 使用Exception对象

Try-Finally 语句

The Try-Finally statement is used when you do not want to provide specific error handling. This statement traps an error, and displays the default error message without terminating the application. When an error is encountered, execution is moved from the Try statement to its Finally clause after Continue is clicked in the default message box. The Try-Finally statement is best used when an application is still being developed and debugged.

当不想提供指定的错误处理程序时使用 Try-Finally 语句。这个语句捕捉一个错误并在没有终止程序的情况下显示一个默认错误信息。当遇到错误时,在默认消息框中点击继续按扭后程序会从 Try 语句移动到 Finally 子句。 Try-Finally 最好是用在程序仍在开发和调试时。

Try-Catch-Finally 语句

The Try-Catch-Finally statement is a combination of the Try-Catch and Try-Finally statements. This statement traps the error and instead of displaying a default error message and terminating the application, execution is moved to the Catch clause of the Try statement. After the code is executed in the Catch clause, execution is moved to the Finally clause which gives your application one last chance to either continue execution or to exit gracefully.

Try-Catch-Finally 语句是 Try-CatchTry-Finally 语句的结合。这个语句捕捉错误然后代替显示默认的错误信息或终止应用程序,然后执行到 Try 语句的 Catch 子句中。在 Catch 子句中的代码执行后,然后会执行到 Finally 子句,它让你决定是继续执行还是正常退出。

测试错误处理,没有或用 Try-Catch-Finally 语句

The following examples attempt to open a file named “Drawing123” on the C: drive. If the file is not found, an eFileNotFound error is thrown. The first command does not catch the error thrown by the ReadDwgFile method, so the default message box is displayed when the command is started in AutoCAD. The second command catches the error thrown using the Try-Catch-Finally statement.

下面的示例尝试打开一个C盘下名字为“Drawing123”的文件。如果文件没有找到,将后抛出 eFileNotFound 错误。第一个命令不能捕捉到 ReadDwgFile 方法抛出的错误,所以当命令在 AutoCAD 中启动时会显示一个默认信息框。第二个命令使用 Try-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)
      '' 读取C盘中名字为"Drawing123.dwg"的图形文件    Read the drawing file named "Drawing123.dwg" on the C: drive.
      '' 如果"Drawing123.dwg"文件不存在,一个eFileNotFound 错误将将会抛出而且程序中止    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
          '' 读取C盘中名字为"Drawing123.dwg"的图形文件    Read the drawing file named "Drawing123.dwg" on the C: drive.
          '' 如果"Drawing123.dwg"文件不存在,一个eFileNotFound 错误将将会抛出而且程序中止    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))
  {
      // 读取C盘中名字为"Drawing123.dwg"的图形文件    Read the drawing file named "Drawing123.dwg" on the C: drive.
      // 如果"Drawing123.dwg"文件不存在,一个eFileNotFound 错误将将会抛出而且程序中止    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
      {
          // 读取C盘中名字为"Drawing123.dwg"的图形文件    Read the drawing file named "Drawing123.dwg" on the C: drive.
          // 如果"Drawing123.dwg"文件不存在,一个eFileNotFound 错误将将会抛出而且程序中止    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");
      }
  }
}