SMO 예외 처리

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

관리 코드에서 오류가 발생하면 예외가 throw됩니다. SMO 메서드와 속성은 반환 값에 성공 또는 실패를 보고하지 않습니다. 대신 예외 처리기에서 예외를 catch하고 처리할 수 있습니다.

SMO에는 다른 예외 클래스가 있습니다. 예외에 대한 문자 메시지를 제공하는 메시지 속성과 같은 예외 속성에서 예외에 대한 정보를 추출할 수 있습니다.

예외 처리 문은 프로그래밍 언어와 관련이 있습니다. 예를 들어 Microsoft Visual Basic에서는 Catch 문입니다.

내부 예외

예외는 일반 또는 특정일 수 있습니다. 일반 예외에는 특정 예외 집합이 포함됩니다. 몇 가지 Catch 문을 사용하여 예상 오류를 처리하고 나머지 오류가 일반적인 예외 처리 코드로 넘어가도록 할 수 있습니다. 예외는 종종 연속 시퀀스에서 발생합니다. SMO 예외가 SQL 예외로 인해 발생하는 경우가 많습니다. 이를 감지하는 방법은 InnerException 속성을 연속적으로 사용하여 최종 최상위 예외를 발생시킨 원래 예외를 확인하는 것입니다.

참고 항목

SQLException 예외는 System.Data.SqlClient 네임스페이스에서 선언됩니다.

A diagram that shows the levels from which an excp

다이어그램은 애플리케이션 계층을 통한 예외 흐름을 보여 줍니다.

제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.

Visual Basic에서 예외 catch

이 코드 예제에서는 Try... 잡을... 마지막으로SMO 예외를 catch하는 Visual Basic 문입니다. 모든 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#에서 예외 catch

이 코드 예제에서는 Try... 잡을... 마지막으로 SMO 예외를 catch하는 Visual C# 문입니다. 모든 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.");   
}   
}