Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all messages from InnerException(s)?

Tags:

c#

c#-4.0

Is there any way to write a LINQ style "short hand" code for walking to all levels of InnerException(s) of Exception thrown? I would prefer to write it in place instead of calling an extension function (as below) or inheriting the Exception class.

static class Extensions {     public static string GetaAllMessages(this Exception exp)     {         string message = string.Empty;         Exception innerException = exp;          do         {             message = message + (string.IsNullOrEmpty(innerException.Message) ? string.Empty : innerException.Message);             innerException = innerException.InnerException;         }         while (innerException != null);          return message;     } };  
like image 316
Jimmy Avatar asked Feb 16 '12 15:02

Jimmy


People also ask

How can I see my InnerException?

When you're debugging and you get an exception, always, always, always click on the View Details link to open the View Details dialog. If the message in the dialog isn't expanded, expand it, then scan down to the Inner Exception entry.

How do I get InnerException in C#?

To get the type of the exception, we can invoke the GetType() method from the Exception class object and then we can invoke the Name property to get the name of the exception. To write the exception details in a file, the StreamWriter provides a method “write” or “writeline” of which we can pass the ex. GetType().

What is inner exception in C#?

Exception. An object that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor. This property is read-only.

Does exception ToString include inner exception?

If you want information about all exceptions then use exception. ToString() . It will collect data from all inner exceptions. If you want only the original exception then use exception.


1 Answers

Unfortunately LINQ doesn't offer methods that could process hierarchical structures, only collections.

I actually have some extension methods that could help do this. I don't have the exact code in hand but they're something like this:

// all error checking left out for brevity  // a.k.a., linked list style enumerator public static IEnumerable<TSource> FromHierarchy<TSource>(     this TSource source,     Func<TSource, TSource> nextItem,     Func<TSource, bool> canContinue) {     for (var current = source; canContinue(current); current = nextItem(current))     {         yield return current;     } }  public static IEnumerable<TSource> FromHierarchy<TSource>(     this TSource source,     Func<TSource, TSource> nextItem)     where TSource : class {     return FromHierarchy(source, nextItem, s => s != null); } 

Then in this case you could do this to enumerate through the exceptions:

public static string GetaAllMessages(this Exception exception) {     var messages = exception.FromHierarchy(ex => ex.InnerException)         .Select(ex => ex.Message);     return String.Join(Environment.NewLine, messages); } 
like image 151
Jeff Mercado Avatar answered Sep 27 '22 22:09

Jeff Mercado