Exception handling pattern

While playing with new Entities in 16 alpha, I got MissigMethodException. It turned out to be thrown from World.cs line 279:

try
{
    system = Activator.CreateInstance(type, constructorArguments) as ComponentSystemBase;
}
catch (MissingMethodException)
{
    throw new MissingMethodException($"Constructing {type} failed because CreateSystem " +
                    $"parameters did not match its constructor.  [Job]ComponentSystem {type} must " +
                    "be mentioned in a link.xml file, or annotated with a [Preserve] attribute to " +
                    "prevent its constructor from being stripped.  See " +
                    "https://docs.unity3d.com/Manual/ManagedCodeStripping.html for more information.");
}

Please take into account there’s innerException property in .NET exception class and a constructor argument. Doing it this way considered bad practice because it destroys all valuable information leaving your users without any clue why the error might happend in the first place. I ask you to replace it with

try
{
    system = Activator.CreateInstance(type, constructorArguments) as ComponentSystemBase;
}
catch (MissingMethodException mme)
{
    throw new MissingMethodException($"Constructing {type} failed because CreateSystem " +
                    $"parameters did not match its constructor.  [Job]ComponentSystem {type} must " +
                    "be mentioned in a link.xml file, or annotated with a [Preserve] attribute to " +
                    "prevent its constructor from being stripped.  See " +
                    "https://docs.unity3d.com/Manual/ManagedCodeStripping.html for more information."
                    , mme);
}

Thank you!

3 Likes

Thanks, good catch! This will be fixed in the next Entities package release.