Title: Recursive function not working for to get innermost exception message
Tags: java;recursion;exception-handling
Question: I have written this method
private string FindInnerExceptionMessage(Exception ex)
string exceptionMsg = string.Empty;
if (ex.InnerException == null)
{
exceptionMsg = ex.Message;
}
else
{
ex = ex.InnerException;
FindInnerExceptionMessage(ex);
}
return exceptionMsg;
However, after that FindInnerExceptionMessage it is stepping to return exceptionMsg and not logging the exact exception message
Comment: I guess I could be wrong but isn't this C# code?  The thread is tagged with java.

Here is the accepted answer: You don't actually assign the return value of your recursive call to anything. As a result, your first call will return String.Empty because the value of FindInnerExceptionMessage(ex.InnerException) is never assigned as the return value (unless the exception passed to the first call has no inner exception, in which case it will work). Try something like this:
private string FindInnerExceptionMessage(Exception ex)
string exceptionMsg = string.Empty;
if (ex.InnerException == null)
exceptionMsg = ex.Message;
else
exceptionMsg = FindInnerExceptionMessage(ex.InnerException);
return exceptionMsg;


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/28/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><recursion><exception-handling>', 'Title': 'Recursive function not working for to get innermost exception message', 'ViewCount': 409, 'id': 13612795, 'user_AboutMe': None, 'user_AccountId': 2057796.0, 'user_DisplayName': 'Shabana', 'user_Reputation': 69.0, 'user_Views': 28.0, 'user_id': 1835076.0}}