Catching Exceptions

During the synchronization process, both expected and unexpected exceptions can occur. You can determine which exceptions are non-critical and can be safely ignored, and which exceptions are critical and should stop the synchronization process. Use the Exceptions class in a Try/Catch block to handle unexpected exceptions in your extensions. For more information about the Try/Catch block, see "Using the Try/Catch Block to Catch Exceptions" in the .NET Framework Developer's Guide or see the MSDN Web site at https://msdn.microsoft.com/library.

Note  Catch only exceptions that you intend to handle. If you are going to throw another exception as part of your exception handler, either throw an UnexpectedDataException exception and include a string message argument to help identify the cause of the exception or re-throw the exception by using the Throw statement without any parameters. This will preserve the stack trace.

The following example shows how to catch both expected and unexpected rules exceptions, log an exception, and determine how these exceptions should be handled.

Note  To use the following example, you must include a reference to the Microsoft.MetadirectoryServices.Logging assembly (logging.dll) in your project. For further information on logging messages, see Logging Extension Messages.

    Public Sub Provision(ByVal mventry As MVEntry) _
       Implements IMVSynchronization.Provision
    
        Dim dn As ReferenceValue
        Dim container As String
        Dim rdn As String
        Dim csentry As CSEntry
    
        Try
            ' Determine the container and relative distinguished name
            ' of the new connector space entry.
            container = "OU=somecontainer,DC=fabrikam,DC=com"
            rdn = "CN=" & mventry("cn").Value
            dn = mventry.ConnectedMAs("Fabrikam AD MA").EscapeDNComponent(rdn).Concat(container)
            csentry = mventry.ConnectedMAs("Fabrikam AD MA").Connectors.StartNewConnector("user")
            csentry.DN = dn
            csentry.CommitNewConnector()
    
        Catch ex As ObjectAlreadyExistsException
            ' Ignore if the object already exists, join rules will join the existing object later
    
        Catch ex As NoSuchAttributeException
            ' Ignore if the attribute on the mventry object is not available at this time
    
        Catch ex As Exception
            ' Log exception, with timestamp, at level 1
            Logging.Logging.Log("Caught exception " & ex.Message, True, 1)
            ' All other exceptions re-throw to rollback the object synchronization transaction and 
            ' report the error to the run history
            Throw
    
        End Try
    
    End Sub
    void IMVSynchronization.Provision (MVEntry mventry)
    {
       ReferenceValue dn;
       String container;
       String rdn;
       CSEntry csentry;    
    
       try
       {
          // Determine the container and relative distinguished name
          // of the new connector space entry.
          container = "OU=somecontainer,DC=fabrikam,DC=com";
          rdn = "CN=" + mventry["cn"].Value;
          dn = mventry.ConnectedMAs["Fabrikam AD MA"].EscapeDNComponent(rdn).Concat(container);
          csentry = mventry.ConnectedMAs["Fabrikam AD MA"].Connectors.StartNewConnector("user");
          csentry.DN = dn;
          csentry.CommitNewConnector();
       }
    
       catch(ObjectAlreadyExistsException)
       {
          // Ignore if the object already exists, join rules will join the existing object later
       }
    
       catch(NoSuchAttributeException)
       {
          // Ignore if the attribute on the mventry object is not available at this time
       }
    
       catch(Exception ex)
       {
          // Log exception, with timestamp, at level 1
          Logging.Logging.Log("Caught exception " + ex.Message, true, 1);
    
          // All other exceptions re-throw to rollback the object synchronization transaction and 
          // report the error to the run history
          throw;
       }
    }

See Also

Handling Exceptions
Example: Checking for Necessary Attributes

Send comments about this topic to Microsoft

Build date: 2/16/2009