0
  1. Is it possible to catch all WCF statements in a single catch statement? - i.e 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 } 
4
  • 4
    possible duplicate of Catch multiple Exceptions at once? Commented Apr 12, 2014 at 12:03
  • thanks - it answers item 1 but not item 2 Commented Apr 12, 2014 at 12:06
  • Is this for a WCF client or server? Your approach will differ depending on the answer. Review this MSDN article: bit.ly/1lXorLn as it will help you in either case. Commented Apr 12, 2014 at 12:24
  • @PeterB It is for a client Commented Apr 12, 2014 at 12:24

1 Answer 1

2

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 (i.e., 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(); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I've updated to show other exceptions. CommunicationException and TimeoutException cover the WCF wire-level errors.
Review the exception defined within the System.ServiceModel namespace. There are a bunch. See msdn.microsoft.com/en-us/library/…. Also, in your general Exception catch block, you can log the exception type as a way to discover any common exceptions for which you may need special error handling.
as far as i can tell your list above catches all of them (especially CommunicationException cathes most exceptions) but i dont know and that link doesnt talk about that , i dont see the point in listing all failure modes if i can cover them under 3 as for me the action to take is the same for all failure modes from WCF.
It really comes down to what exceptions to you want to handle in a special way. You're right, just about all of the WCF exception derive from CommunicationException, so unless you want to take special action for something like ProtocolException, then you don't need to clutter up your code with all possible exceptions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.