How to serialize an Exception object in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Serializing exceptions is useful for logging, diagnostics APIs, and cross-process error reporting. In modern .NET, the safest pattern is serializing a controlled error model instead of trying to serialize runtime exception objects directly. You can still support legacy exception serialization patterns, but they should be used intentionally and with security awareness.
What You Usually Need To Serialize
Most systems do not need a full raw Exception instance. They need stable fields such as:
- exception type
- message
- stack trace
- inner exception chain
- selected context data
Building an explicit error payload avoids brittle serializer behavior and accidental leakage of sensitive fields.
Preferred Approach: Map Exception to DTO
Create a serializable model and map from Exception.
This gives predictable payloads and good forward compatibility.
Why Not Serialize Raw Exception Blindly
Raw exception graphs can include non-serializable members, environment-specific details, and framework internals that change across versions. They may also include data you do not want exposed externally.
For API responses, prefer a constrained contract:
- user-safe message
- error code
- correlation identifier
- optional debug data in internal-only channels
Legacy Exception Serialization in Custom Exceptions
If you build custom exceptions and need compatibility with classic serialization mechanisms, implement serialization constructor and GetObjectData.
Use this only when the transport truly requires that style.
Logging Pipeline Integration
Many teams serialize exceptions for structured logging.
Keep logging and API contracts separate so internal stack traces do not leak to clients.
Handling Inner Exception Depth Safely
Deep inner-exception chains can generate large payloads. Add optional max depth control in your mapper if logs or events have strict size limits. Truncating safely with a marker field is better than dropping the whole error event.
Security and Compliance Notes
Before serializing exception data:
- remove secrets from connection strings and tokens
- avoid dumping raw request bodies in error payloads
- apply redaction policy consistently
Exception serialization is observability, but it is also a data-handling surface.
Common Pitfalls
- Trying to serialize raw exception objects directly into public API responses.
- Using legacy binary serialization in new systems without strong justification.
- Forgetting to include inner exception details when needed for debugging.
- Logging sensitive data from exception context fields without redaction.
- Coupling user-facing error schema to framework exception type names.
Summary
- Prefer mapping exceptions into explicit DTO models for safe serialization.
- Use
System.Text.Jsonfor predictable modern JSON payloads. - Support classic exception serialization only for legacy compatibility needs.
- Separate internal diagnostic detail from external API error contracts.
- Apply redaction and size-control policies to serialized exception data.

