Tuesday, April 26, 2005 3:01 PM jerdenn

Mark custom .NET Exceptions as [Serializable]

The two people that read my blog (besides my sister) know this already, but if you author a class library, please mark all classes deriving from Exception (or even better, ApplicationException) as [Serializable].  If I am using your library on the other side of a remoting channel, non-Serializable exceptions will cause me much heartache.

With Affection,

Your Customer.

 

Filed under:

Comments

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 4:31 PM by Paul Wilson

Sorry for the pain. :)

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 4:52 PM by Jerry Dennany

Ok, I stand corrected. Only one of the two people I know that reads my blog knows better ;-)

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 5:10 PM by Alex Papadimoulis

If you'd like to expand your readership to the double digits, I would recommend explaining the need/benifit to a serializable exception.

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 5:10 PM by Alex Papadimoulis

That was meant to be tounge-in-cheek. I must have left off the ";-)"

;-)

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 5:14 PM by Rolando

Do *NOT* derive your exceptions from ApplicationException.

http://weblogs.asp.net/dotnetrolando/archive/2005/04/11/399846.aspx

Note to Brad: one more evidence that .net docs are taking developers to the wrong conclusion.

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 5:42 PM by Jerry Dennany

Rolando -
There is nothing inherently 'evil' about deriving from ApplicationException. It's an extra level of indirection, but it's all merely a matter of preference.

There are a lot of other things in the Framework that need to be fixed before this.

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 7:05 PM by Rolando

Jerry:
in your post you mention that it's *better* to derive from ApplicationException but according to the .net team: "... it only adds unnecessary depth to the hierarchy". So probably is not evil but certainly is not better.

# re: Mark custom .NET Exceptions as [Serializable]

Tuesday, April 26, 2005 8:24 PM by Paul Lockwood

Alex you missed the word remoting. I had to read the post twice before getting it :)

...don't derive from ApplicationException... gosh I cannot keep up. I must read more blogs, sleep is overrated anyway.

# re: Mark custom .NET Exceptions as [Serializable]

Wednesday, July 01, 2009 4:56 AM by Cheeso

FYI: This is the source of the "don't derive from ApplicationException" note. msdn.microsoft.com/.../ms229064.aspx

BUT, it also points out that decorating the custom exception type declaration with [serializable] is not enough. You must also provide, at least, a ctor that accepts args as (SerializationInfo, StreamingContext) .

See also, winterdom.com/.../makeexceptionclassesserializable

Leave a Comment

(required) 
(required) 
(optional)
(required)