title: Wrapping calls to method on a class with a standard try/catch
body: <p>I have a class that has about 200+ methods, each of these methods makes a call into the database, or a network resource. </p>
  
  <p>Ideally, I would like to wrap each call in a try/catch, to catch any of the common network or SQL exceptions, and give the user the opportunity to try again (if appropriate). However, to add this code to each call would be very time consuming, and bloated as far as the code is concerned.</p>
  
  <p>I've thought about wrapping each of the method calls in another method, creating a delegate, and wrapping the delegate code in the try/catch.. something like this...</p>
  
  <p><strong>(Ignore the syntax.. it's just a conceptual example)</strong></p>
  
  <pre><code>bool CallUpdatePassenger(int PassengerId,string PassengerName,string PhoneNumber)
  {
      Delegate del= Delegate.CreateDelegate(typeof(UpdatePassengerDelegate), typeof(IPassengerServices).GetMethod("RemoteUpdatePassenger"));
      bool Res=(bool)CallDelegate(del,PassengerName,PhoneNumber);
  }
  object CallDelegate(Delegate del,params object[] args)
  {
      object Result=null;
      try
      {
          Result=del.DynamicInvoke(args);
      }
      catch (Some.Timeout.Error.Or.Whatever te)
      {
          // take some action.. maybe retry etc.. 
      }
      return Result;
  }
  </code></pre>
  
  <p>Maybe there's a more pracitcal way of doing this ?</p>
  
  <p>The code is automatically generated (by a tool which I wrote) I can quite easily include something like the above, but I want to avoid writing the above code for every method call.</p>
  
  <p>Also, if I do something like the above, I can time methods, and log method calls etc. It just seems a bit clumsy (and not strongly typed).</p>
  
  <p>Thanks
  Rich.</p>