Add ExceptionDispatchInfo.SetCurrentStackTrace (dotnet/coreclr#27004)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Exception.cs
bloba437da1c33f2563a04ca44a2c2ed610ec4df1b60
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Collections;
6 using System.Diagnostics;
7 using System.Runtime.Serialization;
9 namespace System
11 [Serializable]
12 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
13 public partial class Exception : ISerializable
15 private protected const string InnerExceptionPrefix = " ---> ";
17 public Exception()
19 _HResult = HResults.COR_E_EXCEPTION;
22 public Exception(string? message)
23 : this()
25 _message = message;
28 // Creates a new Exception. All derived classes should
29 // provide this constructor.
30 // Note: the stack trace is not started until the exception
31 // is thrown
33 public Exception(string? message, Exception? innerException)
34 : this()
36 _message = message;
37 _innerException = innerException;
40 protected Exception(SerializationInfo info, StreamingContext context)
42 if (info == null)
43 throw new ArgumentNullException(nameof(info));
45 _message = info.GetString("Message"); // Do not rename (binary serialization)
46 _data = (IDictionary?)(info.GetValueNoThrow("Data", typeof(IDictionary))); // Do not rename (binary serialization)
47 _innerException = (Exception?)(info.GetValue("InnerException", typeof(Exception))); // Do not rename (binary serialization)
48 _helpURL = info.GetString("HelpURL"); // Do not rename (binary serialization)
49 _stackTraceString = info.GetString("StackTraceString"); // Do not rename (binary serialization)
50 _HResult = info.GetInt32("HResult"); // Do not rename (binary serialization)
51 _source = info.GetString("Source"); // Do not rename (binary serialization)
53 RestoreRemoteStackTrace(info, context);
56 public virtual string Message => _message ?? SR.Format(SR.Exception_WasThrown, GetClassName());
58 public virtual IDictionary Data => _data ??= CreateDataContainer();
60 private string GetClassName() => GetType().ToString();
62 // Retrieves the lowest exception (inner most) for the given Exception.
63 // This will traverse exceptions using the innerException property.
64 public virtual Exception GetBaseException()
66 Exception? inner = InnerException;
67 Exception back = this;
69 while (inner != null)
71 back = inner;
72 inner = inner.InnerException;
75 return back;
78 public Exception? InnerException => _innerException;
80 // Sets the help link for this exception.
81 // This should be in a URL/URN form, such as:
82 // "file:///C:/Applications/Bazzal/help.html#ErrorNum42"
83 public virtual string? HelpLink
85 get => _helpURL;
86 set => _helpURL = value;
89 public virtual string? Source
91 get => _source ??= CreateSourceName();
92 set => _source = value;
95 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
97 if (info == null)
99 throw new ArgumentNullException(nameof(info));
102 if (_source == null)
104 _source = Source; // Set the Source information correctly before serialization
107 info.AddValue("ClassName", GetClassName(), typeof(string)); // Do not rename (binary serialization)
108 info.AddValue("Message", _message, typeof(string)); // Do not rename (binary serialization)
109 info.AddValue("Data", _data, typeof(IDictionary)); // Do not rename (binary serialization)
110 info.AddValue("InnerException", _innerException, typeof(Exception)); // Do not rename (binary serialization)
111 info.AddValue("HelpURL", _helpURL, typeof(string)); // Do not rename (binary serialization)
112 info.AddValue("StackTraceString", SerializationStackTraceString, typeof(string)); // Do not rename (binary serialization)
113 info.AddValue("RemoteStackTraceString", SerializationRemoteStackTraceString, typeof(string)); // Do not rename (binary serialization)
114 info.AddValue("RemoteStackIndex", 0, typeof(int)); // Do not rename (binary serialization)
115 info.AddValue("ExceptionMethod", null, typeof(string)); // Do not rename (binary serialization)
116 info.AddValue("HResult", _HResult); // Do not rename (binary serialization)
117 info.AddValue("Source", _source, typeof(string)); // Do not rename (binary serialization)
118 info.AddValue("WatsonBuckets", SerializationWatsonBuckets, typeof(byte[])); // Do not rename (binary serialization)
121 public override string ToString()
123 string s = GetClassName();
125 string? message = Message;
126 if (!string.IsNullOrEmpty(message))
128 s += ": " + message;
131 if (_innerException != null)
133 s += Environment.NewLineConst + InnerExceptionPrefix + _innerException.ToString() + Environment.NewLineConst + " " + SR.Exception_EndOfInnerExceptionStack;
136 string? stackTrace = StackTrace;
137 if (stackTrace != null)
139 s += Environment.NewLineConst + stackTrace;
142 return s;
145 protected event EventHandler<SafeSerializationEventArgs>? SerializeObjectState
147 add { throw new PlatformNotSupportedException(SR.PlatformNotSupported_SecureBinarySerialization); }
148 remove { throw new PlatformNotSupportedException(SR.PlatformNotSupported_SecureBinarySerialization); }
151 public int HResult
153 get => _HResult;
154 set => _HResult = value;
157 // this method is required so Object.GetType is not made virtual by the compiler
158 // _Exception.GetType()
159 public new Type GetType() => base.GetType();
161 partial void RestoreRemoteStackTrace(SerializationInfo info, StreamingContext context);