How to set the InnerException of custom Exception class from its constructor
You set the inner exception by calling the base ctor:
public MyException(string message, Exception innerException)
: base(message, innerException) {...}
If you need to run some code to get the exception, use a static method:
public MyException(SomeData data) : base(GetMessage(data), GetInner(data)) {...}
static Exception GetInner(SomeData data) {...} // <===== your type creation here!
static string GetMessage(SomeData data) {...}
The Exception
class has an overloaded constructor accepting the inner exception as a parameter:
Exception exc = new Exception("message", new Exception("inner message"));
Is this what you are looking for?