What is an inner exception, and how do you use it?
Quick Answer
The inner exception is the original exception preserved inside a new one you throw to add context: throw new OrderProcessingException("failed to save order 5", ex). It gives callers a readable high-level message while keeping the root cause and its stack trace for debugging.
When I catch an exception and want to rethrow with more context, I create a new exception with a meaningful message and pass the original as the inner exception. So the top-level message is “couldn't process order 5” but InnerException still holds the SqlException with its stack trace. A logger should always dump the whole chain, not just the outer message. And AggregateException from parallel work is the multi-inner-exception version — you flatten it to see them all.
- Read it via
ex.InnerException; walk the whole chain withex.GetBaseException(). AggregateException(fromTask.WhenAll, parallel loops) holds multipleInnerExceptions— flatten with.Flatten().- Loggers and error pages should print the full chain; showing only the outer message hides the actual problem.