1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
16 using System.Linq.Expressions;
18 using Microsoft.Scripting.Ast;
22 using System.Collections.Generic;
23 using System.Diagnostics;
24 using System.Reflection;
25 using System.Threading;
26 using Microsoft.Scripting.Actions;
27 using Microsoft.Scripting.Generation;
29 namespace Microsoft.Scripting.Runtime {
30 public static class ExceptionHelpers {
31 #if FEATURE_STACK_TRACE
32 private const string prevStackTraces = "PreviousStackTraces";
35 /// Updates an exception before it's getting re-thrown so
36 /// we can present a reasonable stack trace to the user.
38 public static Exception UpdateForRethrow(Exception rethrow) {
40 List<StackTrace> prev;
42 // we don't have any dynamic stack trace data, capture the data we can
43 // from the raw exception object.
44 StackTrace st = new StackTrace(rethrow, true);
46 if (!TryGetAssociatedStackTraces(rethrow, out prev)) {
47 prev = new List<StackTrace>();
48 AssociateStackTraces(rethrow, prev);
58 /// Returns all the stack traces associates with an exception
60 public static IList<StackTrace> GetExceptionStackTraces(Exception rethrow) {
61 List<StackTrace> result;
62 return TryGetAssociatedStackTraces(rethrow, out result) ? result : null;
65 private static void AssociateStackTraces(Exception e, List<StackTrace> traces) {
66 e.Data[prevStackTraces] = traces;
69 private static bool TryGetAssociatedStackTraces(Exception e, out List<StackTrace> traces) {
70 traces = e.Data[prevStackTraces] as List<StackTrace>;
71 return traces != null;
74 public static Exception UpdateForRethrow(Exception rethrow) {