Is it possible to output all error messages of thrown exception including inners via LINQ?
I implemented the functionality without LINQ, but I would like to have more concise code. (Isn't the purpose of the LINQ?)
My code without LINQ follows:
try {
...
} catch (Exception ex) {
string msg = "Exception thrown with message(s): ";
Exception curEx= ex;
do {
msg += string.Format("\n {0}", curEx.Message);
curEx = curEx.InnerException;
} while (curEx != null);
MessageBox.Show(msg);
}
Linq works on sequences, ie., collections of objects. The exception.InnerException
hierarchy is instead nested instances of single objects. What you are doing algorithmically is not inherently a sequence operation, and would not be covered by Linq approaches.
You could define a method that explores the hierarchy and returns (yields) a sequence of objects as they are found, but this ultimately is going to be the same algorithm you're currently using to explore the depths, although you could then choose to apply a sequence operation (Linq) on the results.
To follow up on @Anthony Pegram's answer, you could define an extension method to get a sequence of the inner exceptions:
public static class ExceptionExtensions
{
public static IEnumerable<Exception> GetAllExceptions(this Exception ex)
{
List<Exception> exceptions = new List<Exception>() {ex};
Exception currentEx = ex;
while (currentEx.InnerException != null)
{
currentEx = currentEx.InnerException;
exceptions.Add(currentEx);
}
return exceptions;
}
}
then you would be able to use LINQ on the sequence. If we have a method that throws nested exceptions like this:
public static class ExceptionThrower {
public static void ThisThrows() {
throw new Exception("ThisThrows");
}
public static void ThisRethrows() {
try {
ExceptionThrower.ThisThrows();
}
catch (Exception ex) {
throw new Exception("ThisRetrows",ex);
}
}
}
here's how you can use LINQ with the little extension method we created:
try {
ExceptionThrower.ThisRethrows();
}
catch(Exception ex) {
// using LINQ to print all the nested Exception Messages
// separated by commas
var s = ex.GetAllExceptions()
.Select(e => e.Message)
.Aggregate((m1, m2) => m1 + ", " + m2);
Console.WriteLine(s);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With