VB.NET および C# では、Try 文を使用して実行時エラーをトラップできます。この文では、文字通りにシステムにトラップを設定します。Try 文でエラーが発生すると、システムの既定のエラー処理がバイパスされて、実行は Catch 句にリダイレクトされます。
Try 文には 3 つの形式があります。
Try-Catch 文は、エラーに応答する場合に使用します。この文は、エラーをトラップすると、既定のエラー メッセージを表示してアプリケーションを終了する代わりに、Try 文の Catch 句に実行を移動します。
Catch 句は、オプションとして、Exception オブジェクトを受け取る 1 つのパラメータを含むことができます。Exception オブジェクトは、発生したエラーについての情報を含みます。発生したエラーを解決できない場合は、カスタム エラー メッセージを表示し、アプリケーションを正常に終了する必要があります。
Try-Finally 文は、特定のエラー処理を提供しない場合に使用します。この文は、エラーをトラップすると、アプリケーションを終了しないで既定のエラー メッセージを表示します。エラーが発生すると、既定のメッセージ ボックスで[続行]がクリックされた後、実行は Try 文から Finally 句に移動します。Try-Finally 文は、アプリケーションがまだ開発およびデバッグ中のときに使用すると最適です。
Try-Catch-Finally 文は、Try-Catch 文と Try-Finally 文を組み合わせたものです。この文は、エラーをトラップすると、既定のエラー メッセージを表示してアプリケーションを終了する代わりに、Try 文の Catch 句に実行を移動します。Catch 句でコードが実行された後、実行は Finally 句に移動し、そこでアプリケーションは実行の続行または正常な終了を最終的に決定できます。
次の例では、C: ドライブにある Drawing123 という名前のファイルを開きます。ファイルが見つからない場合は、eFileNotFound エラーをスローします。1 番目のコマンドは ReadDwgFile メソッドによってスローされるエラーを検出しないので、AutoCAD でコマンドが開始されると既定のメッセージ ボックスが表示されます。2 番目のコマンドは、Try-Catch-Finally 文を使用してスローされたエラーを検出します。
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
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");
}
}
}