Export (0) Print
Expand All

Exception Handling in C#

The following example shows how to handle Windows Communication Foundation (WCF) exceptions. For more information, see Handling Service Errors and Exceptions.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.


try
{
    _service = new CampaignManagementServiceClient();

    // Process application and call subroutines that handle Bing Ads API faults.

    _service.Close();
}
catch (CommunicationException e)
{
    Console.WriteLine(e.Message);

    if (null != e.InnerException)
    {
        Console.WriteLine("\n" + e.InnerException.Message);
    }

    if (_service != null)
    {
        _service.Abort();
    }
}
catch (TimeoutException e)
{
    Console.WriteLine(e.Message);

    if (_service != null)
    {
        _service.Abort();
    }
}
catch (Exception e)
{
    // Ignore fault exceptions that we already caught.

    if (!(e.InnerException is FaultException))
    {
        Console.WriteLine(e.Message);
    }

    if (_service != null)
    {
        _service.Abort();
    }
}

Community Additions

Show:
© 2014 Microsoft