Are all .NET exceptions serializable?

Yes and no. As Jeff and others have pointed out, the all exception classes should and almost always are serializable. If you run across a particular exception class that is not serializable, it is quite possibly a bug.

But when considering serializability you need to consider both the immediate class and all types which are members of this type (this is a recursive process). The base Exception class has a Data property which is of type IDictionary. This is problematic because it allows you, or anyone else, to add any object into the Exception. If this object is not serializable then serialization of the Exception will fail.

The default implementation of the Data property does do some basic checking to ensure an object being added is serializable. But it is flawed in several ways

  • Only does top level checking for serializability of a type and this check is only done by checking Type.IsSerializable (not a 100% solid check). 
  • The property is virtual. A derived exception type could override and use a Hashtable which does 0 checking.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)