You could use Task.Run(() => throw new Exception());
for example. This will throw an AggregateException
which will contain the exception as an inner exception.
Invoking things that throw exceptions via reflection will also cause a TargetInvocationException
to be thrown containing the actual exception as an inner exception.
Using the XmlSerializer
to deserialize an invalid XML file usally produces a more deeply nested error hierarchy if I recall correctly.
For example the following program will throw an exception three "levels" deep:
public class MyClass
{
[XmlElement("Element")]
int Element { get; set; }
}
class Program
{
static void Main(string[] args)
{
string xml = "<Element>String</Element>";
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
serializer.Deserialize(new StringReader(xml));
}
}
But by far the simplest solution of course is to throw your own nested exception.