Search Unity

Exception handling pattern

Discussion in '2020.1 Beta' started by palex-nx, Dec 17, 2019.

  1. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,727
    While playing with new Entities in 16 alpha, I got MissigMethodException. It turned out to be thrown from World.cs line 279:

    Code (CSharp):
    1.  
    2. try
    3. {
    4.     system = Activator.CreateInstance(type, constructorArguments) as ComponentSystemBase;
    5. }
    6. catch (MissingMethodException)
    7. {
    8.     throw new MissingMethodException($"Constructing {type} failed because CreateSystem " +
    9.                     $"parameters did not match its constructor.  [Job]ComponentSystem {type} must " +
    10.                     "be mentioned in a link.xml file, or annotated with a [Preserve] attribute to " +
    11.                     "prevent its constructor from being stripped.  See " +
    12.                     "https://docs.unity3d.com/Manual/ManagedCodeStripping.html for more information.");
    13. }
    14.  
    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

    Code (CSharp):
    1.  
    2. try
    3. {
    4.     system = Activator.CreateInstance(type, constructorArguments) as ComponentSystemBase;
    5. }
    6. catch (MissingMethodException mme)
    7. {
    8.     throw new MissingMethodException($"Constructing {type} failed because CreateSystem " +
    9.                     $"parameters did not match its constructor.  [Job]ComponentSystem {type} must " +
    10.                     "be mentioned in a link.xml file, or annotated with a [Preserve] attribute to " +
    11.                     "prevent its constructor from being stripped.  See " +
    12.                     "https://docs.unity3d.com/Manual/ManagedCodeStripping.html for more information."
    13.                     , mme);
    14. }
    15.  
    Thank you!
     
    LeonhardP, Xarbrough and Prodigga like this.
  2. cort_of_unity

    cort_of_unity

    Unity Technologies

    Joined:
    Aug 15, 2018
    Posts:
    25
    Thanks, good catch! This will be fixed in the next Entities package release.
     
unityunity