The stackoverflow limit is a bit severe, you'd probably have to go at least over 1000 exceptions to be in stackoverflow land. Moreover, i'd actually rather get some kind of exception over not getting a full stack trace. It'd probably be better to throw an InvalidOperationException instead of just bailing at the limit of 50.
You should also use recursion over your loop. I submit my suggestion for improvement (although it's in C#)
private static string GetFullStackTrace(Exception ex )
{
int ctr = 0;
return GetFullStackTrace( new StringBuilder(), ex, ref ctr );
}
private static string GetFullStackTrace( StringBuilder sb, Exception ex, ref int ctr )
{
if ( sb == null ) { throw new ArgumentNullException( "sb" ); }
if ( ex == null ) { throw new ArgumentNullException( "ex" ); }
if ( ctr > 49 ) { throw new InvalidOperationException( "Over 50 inner exceptions found in exception. Please re-consider your crazy architecture" ); }
sb.Insert( 0, ex.StackTrace );
ctr++;
if ( ex.InnerException != null )
{
return GetFullStackTrace( sb, ex.InnerException, ref ctr );
}
else
{
return sb.ToString();
}
}
It'd probably be better to throw an InvalidOperationException instead of just bailing at the limit of 50.
You should also use recursion over your loop.
I submit my suggestion for improvement (although it's in C#)