TIP: Decorate Custom Exception Classes With the Serializable Attribute

archived comments edit

When writing a custom Exception class, don’t forget to mark the class as Serializable? Why? If the exception is ever used in a remoting context, exceptions on the server are serialized and remoted back to the client proxy. The proxy then throws the exception on the client. By default, .NET types are not serializable. But by adding a simple Serializable attribute decoration on your class, .NET is able to serialize your exception.

[Serializable]
public class MyException : Exception
{
  // Custom Code

}

Technorati Tags: Tips

Comments