Avoid throwing exceptions when deserializing

2 posts, 0 answers
  1. Rock
    Rock avatar
    4 posts
    Member since:
    Sep 2013

    Posted 08 Oct 2013 Link to this post

    In our organization, we break when exceptions are thrown. This allows us to improve our code by eliminating exception-throwing as much as possible. Not a problem for our customers, but we don't want to turn this off for our developers, who use it to find problems in the code.

    Because of this, we discovered that the Dock code always throws an exception when deserializing the layout because it's trying to create an Image type. Image is abstract, so it throws an exception, catches it, and continues on its way. The code below (from ComponnentXmlSerializer.cs) could be improved by first checking if the type has a paramterless constructor, and then not catching the MissingMethodException. This would speed up deserialization and let us keep "break on exceptions" turned on.

    try
    {
        newInstance = this.InstanceFactory.CreateInstance(type);
    }
    catch (TargetInvocationException)
    {
        return;
    }
    catch (MissingMethodException)
    {
        return;
    }
  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 11 Oct 2013 Link to this post

    Hello Rock,

    Thank you for contacting Telerik support.

    We have considered your suggestion and we have updated our code as follows:
    ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes);
    if (constructorInfo != null && constructorInfo.IsPublic )
    {
        object newInstance = null;
        try
        {
     
            newInstance = this.InstanceFactory.CreateInstance(type);
        }
        catch (TargetInvocationException)
        {
            return;
        }
        catch (MissingMethodException)
        {
            return;
        }
    }

    Your Telerik Points have been updated for this report.

    Please let me know if there is something else I can help you with. 

    Regards,
    Dimitar
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top