Search code examples
c#.net.net-standard.net-standard-1.5

Implementing Exceptions in .NET Standard 1.5


How do I implement a new Exception when I am targeting .NET Standard 1.5 and earlier versions? The [Serializable] attribute is not available and my understanding is that this is required for the full .NET framework.

[Serializable]
public class MyException : Exception
{
   // ...
}

Solution

  • The full .NET Framework has a best practice to implement the three magic constructors and annotate it with the Serializable attribute. To my knowledge this was due to remoting and app domains. But that does not prevent you from to deliver an exception without serializable attribute.

    However, you have to be aware, that serialization of that exception will fail in case serialization is applied (app Domains, remoting, ...).

    The CoreFx library part of the .NET Core project does not annotate exception classes with the serializable Attribute.

    The .NET Core team is aware of the problem.

    The Serializable attribute is now available in .NET Standard 2.0.