Skip to content

Commit

Permalink
Ignore empty stack frames when printing stack trace from previous loc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
marek-safar committed Jan 3, 2013
1 parent 5b177df commit 966ddd5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mcs/class/corlib/System/Exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ public virtual string Source {
}
}

void AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st) {
for (int i = 0; i < st.FrameCount; i++) {
bool AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st)
{
int i;
for (i = 0; i < st.FrameCount; i++) {
StackFrame frame = st.GetFrame (i);
if (i == 0)
sb.AppendFormat (" {0} ", Locale.GetText ("at"));
Expand All @@ -214,6 +216,8 @@ void AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st)
frame.GetFileLineNumber ());
}
}

return i != 0;
}

public virtual string StackTrace {
Expand All @@ -233,7 +237,9 @@ public virtual string StackTrace {
// Add traces captured using ExceptionDispatchInfo
if (captured_traces != null) {
foreach (var t in captured_traces) {
AddFrames (sb, newline, unknown, t);
if (!AddFrames (sb, newline, unknown, t))
continue;

sb.Append (Environment.NewLine);
sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
sb.Append (Environment.NewLine);
Expand Down Expand Up @@ -379,11 +385,10 @@ internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
}

// For ExceptionDispatchInfo
internal void CaptureTrace () {
internal void CaptureTrace ()
{
if (captured_traces != null) {
StackTrace[] new_traces = new StackTrace [captured_traces.Length + 1];
Array.Copy (captured_traces, new_traces, captured_traces.Length);
captured_traces = new_traces;
Array.Resize (ref captured_traces, captured_traces.Length + 1);
} else {
captured_traces = new StackTrace [1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void Throw ()
var orig_stack = orig.StackTrace;
try {
ed.Throw ();
Assert.Fail ("#0");
} catch (Exception e) {
var s = e.StackTrace.Split ('\n');
Assert.AreEqual (4, s.Length, "#1");
Expand All @@ -81,6 +82,18 @@ public void Throw ()
}
}

[Test]
public void ThrowWithEmptyFrames ()
{
var edi = ExceptionDispatchInfo.Capture (new OperationCanceledException ());
try {
edi.Throw ();
Assert.Fail ("#0");
} catch (OperationCanceledException e) {
Assert.IsFalse (e.StackTrace.Contains ("---"));
}
}

}
}

Expand Down

0 comments on commit 966ddd5

Please sign in to comment.