다음을 통해 공유


SMO 예외 처리

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

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

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

내부 예외

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

비고

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

excp

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

예시

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

Visual Basic에서 예외 catch

이 코드 예제에서는 Visual Basic 문을 사용하여 Try...Catch...FinallySMO 예외를 catch하는 방법을 보여줍니다. 모든 SMO 예외에는 SmoException 형식이 있으며 SMO 참조에 나열됩니다. 오류의 루트를 표시하기 위해 내부 예외 시퀀스가 표시됩니다. 자세한 내용은 Visual Basic .NET 설명서를 참조하세요.

Visual C에서 예외 catch#

이 코드 예제에서는 Visual C# 문을 사용하여 Try...Catch...Finally SMO 예외를 catch하는 방법을 보여줍니다. 모든 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.");   
}   
}