简体   繁体   中英

Catch all WCF exceptions in a single catch statement

  1. Is it possible to catch all WCF statements in a single catch statement? - ie on the code below i have 2 catches for WCF but my codes reaction to both is the same so i dont want to duplicate code
  2. Will both WCF catches, catch ALL WCF errors or am i missing any ?

Note i have seen these list here

try
       {
        // Some code......
       }
       catch (CommunicationException exception) // WCF Exception 
            {

            }

      catch (TimeoutException exception) // WCF Exception - 
            {

            }

      catch (Exception ex)
            {
                // Standard exception
            }

In a WCF Client, you can capture exceptions thrown from a service catching a FaultException. You can also catch any other class of error if you want special handling (ie, TimeoutException or CommunicationException).

Here's an example:

proxy ServiceClient();
try
{
    proxy = new ServiceClient();
    proxy.DoSomething();
}
catch (FaultException ex)
{
   // handle errors returned by WCF service
}
catch (CommunicationException ex)
{
  // handle communication errors here 
}
catch (TimeOutException ex)
{
  // handle timeouts here 
}
catch (Exception ex)
{
  // handle unaccounted for exception here 
}
finally
{
   if (proxy.State == CommunicationState.Opened)
   {
      proxy.Close();
   }
   else
   {
      proxy.Abort();
   }     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM