Exceptions are Easy. Except when they’re not.

Assuming all the code compiles correctly, is Foo.Load() exception safe? Is there anything I need to change in Load’s exception handler to insure an instance of Foo is always in a valid state?

///Defines a type that associates a URI with the
/// count of frobs, miscellaneous information about those frobs
/// and where to get them.
///
public class Foo
{
    public Foo()
    {
        Miscellaneous = new Dictionary<string , object>();
    }

    private Uri _infoSource;
    public Uri InfoSourceUri
    {
       get { return _infoSource; }
       set
       {
         _infoSource = value;
         Load();
       }
    }

    private void Load()
    {

        ServiceProxy<IFooConfig> proxy = null;
        try
        {
            proxy = ServiceHelper.CreateProxyFromServiceContractInf<IFooConfig>(InfoSourceUri.ToString());
            IFooConfig config = proxy.ProxyContract;

            NumberOfFrobs = config.NumberOfFrobs;
            WhereToGetThem = config.FrobSource;
            for (var e in config.MiscellaneousEntries)
            {
                Miscellaneous.Add(e);
            }
        }
        catch (Exception ex)
        {
            // Report the error
        }
        finally
        {
            proxy.CleanupProxy(); // Extension method that aborts if proxy is
                                  // faulted or ignores if null
        }
    }

    public int NumberOfFrobs { get; set; }
    public Uri WhereWeGetThem { get; set; }
    public IDictionary<string , object> Miscellaneous { get; private set; }
}

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. [...] a previous post, I posed two questions to my readers (all √(-1) of them): whether a body of code was exception [...]

    Pingback by mentis vulgaris » Exceptions are Easy. Except When They’re Not (Part 2) — March 2, 2010 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments links could be nofollow free.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.