TIP: Decorate Custom Exception Classes With the Serializable Attribute

0 comments suggest 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

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

One response

  1. Avatar for fsnk
    fsnk June 20th, 2008

    fanculo