SMO 例外の処理

マネージ コードでは、エラーが発生すると例外がスローされます。 SMO のメソッドやプロパティは、戻り値で成功や失敗をレポートしません。 代わりに、例外ハンドラーによって例外のキャッチと処理を行うことができます。

SMO にはさまざまな例外クラスが存在します。 例外の詳細は、例外に関するテキスト メッセージを指定している Message プロパティなどの例外プロパティから抽出することができます。

例外処理ステートメントは、プログラミング言語に固有です。 たとえば、Microsoft Visual Basic では、Catch ステートメントとなります。

内部例外

例外は、一般または固有のどちらかです。 一般例外には、固有の例外のセットが含まれています。 いくつかの Catch ステートメントを使用して、予想されるエラーの処理を行い、残りのエラーを一般例外の処理コードでは処理されないようにすることができます。 例外は、連鎖シーケンスによってしばしば発生します。 SMO 例外が、別の SQL 例外によって生じていることが少なくありません。 これを検出する方法は、InnerException プロパティを連続的に使用して、最終的なトップレベル例外を発生している元の例外を判断します。

注意

SQLException 例外は System.Data.SqlClient 名前空間内で宣言します。

例外が処理される各レベルを示す図

このダイアグラムは、アプリケーションの層を通じた例外のフローを示しています。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「Visual Studio .NET での Visual C# SMO プロジェクトの作成」または「Visual Studio .NET での Visual Basic SMO プロジェクトの作成」を参照してください。

Visual Basic での例外のキャッチ

このコード例では、Try...Catch...Finally Visual Basic ステートメントを使用して SMO 例外をキャッチする方法を示しています。 SMO 例外はすべて SmoException 型であり、これらは SMO のリファレンスに一覧されています。 エラーの原因を示すために、内部例外のシーケンスが表示されます。 詳細については、Visual Basic .NET のマニュアルを参照してください。

'This sample requires the Microsoft.SqlServer.Management.Smo.Agent namespace is included.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define an Operator object variable by supplying the parent SQL Agent and the name arguments in the constructor.
'Note that the Operator type requires [] parenthesis to differentiate it from a Visual Basic key word.
Dim op As [Operator]
op = New [Operator](srv.JobServer, "Test_Operator")
op.Create()
'Start exception handling.
Try
    'Create the operator again to cause an SMO exception.
    Dim opx As OperatorCategory
    opx = New OperatorCategory(srv.JobServer, "Test_Operator")
    opx.Create()
    'Catch the SMO exception
Catch smoex As SmoException
    Console.WriteLine("This is an SMO Exception")
    'Display the SMO exception message.
    Console.WriteLine(smoex.Message)
    'Display the sequence of non-SMO exceptions that caused the SMO exception.
    Dim ex As Exception
    ex = smoex.InnerException
    Do While ex.InnerException IsNot (Nothing)
        Console.WriteLine(ex.InnerException.Message)
        ex = ex.InnerException
    Loop
    'Catch other non-SMO exceptions.
Catch ex As Exception
    Console.WriteLine("This is not an SMO exception.")
End Try

Visual C# での例外のキャッチ

このコード例では、Try...Catch...Finally Visual C# ステートメントを使用して SMO 例外をキャッチする方法を示しています。 SMO 例外はすべて SmoException 型であり、これらは SMO のリファレンスに一覧されています。 エラーの原因を示すために、内部例外のシーケンスが表示されます。 詳細については、C# のドキュメントを参照してください。

{ 
//This sample requires the Microsoft.SqlServer.Management.Smo.Agent namespace to be included. 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Define an Operator object variable by supplying the parent SQL Agent and the name arguments in the constructor. 
//Note that the Operator type requires [] parenthesis to differentiate it from a Visual Basic key word. 
op = new Operator(srv.JobServer, "Test_Operator"); 
op.Create(); 
//Start exception handling. 
try { 
    //Create the operator again to cause an SMO exception. 
    OperatorCategory opx; 
    opx = new OperatorCategory(srv.JobServer, "Test_Operator"); 
    opx.Create(); 
} 
//Catch the SMO exception 
catch (SmoException smoex) { 
    Console.WriteLine("This is an SMO Exception"); 
   //Display the SMO exception message. 
   Console.WriteLine(smoex.Message); 
   //Display the sequence of non-SMO exceptions that caused the SMO exception. 
   Exception ex; 
   ex = smoex.InnerException; 
   while (!object.ReferenceEquals(ex.InnerException, (null))) { 
      Console.WriteLine(ex.InnerException.Message); 
      ex = ex.InnerException; 
    } 
    } 
   //Catch other non-SMO exceptions. 
   catch (Exception ex) { 
      Console.WriteLine("This is not an SMO exception."); 
} 
}