分享方式:


處理 SMO 例外狀況

適用於:SQL Server Azure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse Analytics

在 Managed 程式代碼中,發生錯誤時會擲回例外狀況。 SMO 方法和屬性不會報告傳回值中的成功或失敗。 相反地,例外狀況處理程式可以攔截和處理例外狀況。

SMO 中存在不同的例外狀況類別。 例外狀況的相關信息可以從例外狀況屬性擷取,例如 Message 屬性,以提供例外狀況的相關文字訊息。

例外狀況處理語句是程式設計語言特有的。 例如,在 Microsoft Visual Basic 中,它是 Catch 語句。

內部例外狀況

例外狀況可以是一般或特定。 一般例外狀況包含一組特定的例外狀況。 數 個 Catch 語句可用來處理預期的錯誤,並讓剩餘的錯誤落入一般例外狀況處理程式代碼。 例外狀況通常會在串聯序列中發生。 通常,SMO 例外狀況可能是由 SQL 例外狀況所造成。 偵測到此狀況的方式是連續使用 InnerException 屬性來判斷造成最終最上層例外狀況的原始例外狀況。

注意

SQLException 例外狀況會在 System.Data.SqlClient 命名空間中宣告。

此圖顯示 excp 的來源層級

此圖顯示透過應用程式層的例外狀況流程。

範例

若要使用提供的任何程式代碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在Visual Studio .NET 中建立Visual C# SMO 專案。

在 Visual Basic 中攔截例外狀況

此程式代碼範例示範如何使用 Try...抓住。。。最後,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...抓住。。。 最後,Visual C# 語句可攔截 SMO 例外狀況。 所有 SMO 例外狀況都有 SmoException 類型,並列在 SMO 參考中。 會顯示內部例外狀況的順序,以顯示錯誤的根目錄。 如需詳細資訊,請參閱 Visual 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.");   
}   
}