title: Catching WCF exceptions on the UI side
body: <p>I am trying to figure out the best way to catch WCF exceptions on the UI side.</p>
  
  <p>First, I have to say that <strong>I still do use a <code>using</code> block</strong> when calling my service methods, because my client proxies all implement the <code>IDisposable.Dispose</code> method, like this :</p>
  
  <pre><code>protected virtual void Dispose(bool disposing)
  {
      if (disposing)
      {
          try
          {
              if (State != CommunicationState.Faulted)
              {
                  Close();
              }
          }
          finally
          {
              if (State != CommunicationState.Closed)
              {
                  Abort();
              }
          }
      }
  }
  </code></pre>
  
  <p>This allows me to use a <code>using</code> block in my client code, because at the end of the using block, the Dispose method checks the state of the communication channel upon disposing and aborts the channel if it is on the faulted state.</p>
  
  <p>I have read a lot of articles about catching exceptions on the UI side and actually, my code looks like this :</p>
  
  <pre><code>using (var myClientProxy = new MyClientProxy())
  {
      try
      {
          myClientProxy.DoSomething();
      }
      catch (TimeoutException timeoutException)
      {
          MessageBox.Show(
              timeoutException.Message, 
              "Timeout expired", 
              MessageBoxButtons.OK, 
              MesssageBoxIcon.Error);
      }
      catch (FaultException&lt;ServiceErrorDetails&gt; faultException)
      {
          MessageBox.Show(
              faultException.Detail.Message, 
              "A service exception occured", 
              MessageBoxButtons.OK, 
              MessageBoxIcon.Error);
      }
      catch (CommunicationException communicationException)
      {
          MessageBox.Show(
              communicationException.Message, 
              "Communication problem", 
              MessageBoxButtons.OK, 
              MessageBoxIcon.Error);
      }
      catch (Exception exception)
      {
          MessageBox.Show(
              exception.Message, 
              "An exception occured", 
              MessageBoxButtons.OK, 
              MessageBoxIcon.Error);
      }
  }
  </code></pre>
  
  <p>As you can see, this is very redundant, and, because I have multiple WCF calls from the UI, this can become a quite big code smell.</p>
  
  <p>I would like to be able to catch <strong>anything</strong> that can go bad in the service call in a <strong>single</strong> catch block and show a message box to the user.</p>
  
  <p>Is it good practice ? Because, even if I reduce the code to a single try/catch block, wouldn't that still be a code smell ?</p>
  
  <p>How can I handle that better ?</p>