This is a migrated thread and some comments may be shown as answers.

Avoid throwing exceptions when deserializing

1 Answer 41 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Rock
Top achievements
Rank 1
Rock asked on 08 Oct 2013, 08:25 PM
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;
}

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 11 Oct 2013, 02:15 PM
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 >>
Tags
Dock
Asked by
Rock
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or