2
0

Exception.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.
  4. using System.Collections;
  5. using System.Runtime.Serialization;
  6. namespace System
  7. {
  8. [Serializable]
  9. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  10. public partial class Exception : ISerializable
  11. {
  12. private protected const string InnerExceptionPrefix = " ---> ";
  13. public Exception()
  14. {
  15. _HResult = HResults.COR_E_EXCEPTION;
  16. }
  17. public Exception(string? message)
  18. : this()
  19. {
  20. _message = message;
  21. }
  22. // Creates a new Exception. All derived classes should
  23. // provide this constructor.
  24. // Note: the stack trace is not started until the exception
  25. // is thrown
  26. //
  27. public Exception(string? message, Exception? innerException)
  28. : this()
  29. {
  30. _message = message;
  31. _innerException = innerException;
  32. }
  33. protected Exception(SerializationInfo info, StreamingContext context)
  34. {
  35. if (info == null)
  36. throw new ArgumentNullException(nameof(info));
  37. _message = info.GetString("Message"); // Do not rename (binary serialization)
  38. _data = (IDictionary?)(info.GetValueNoThrow("Data", typeof(IDictionary))); // Do not rename (binary serialization)
  39. _innerException = (Exception?)(info.GetValue("InnerException", typeof(Exception))); // Do not rename (binary serialization)
  40. _helpURL = info.GetString("HelpURL"); // Do not rename (binary serialization)
  41. _stackTraceString = info.GetString("StackTraceString"); // Do not rename (binary serialization)
  42. _HResult = info.GetInt32("HResult"); // Do not rename (binary serialization)
  43. _source = info.GetString("Source"); // Do not rename (binary serialization)
  44. RestoreRemoteStackTrace(info, context);
  45. }
  46. public virtual string Message
  47. {
  48. get
  49. {
  50. return _message ?? SR.Format(SR.Exception_WasThrown, GetClassName());
  51. }
  52. }
  53. public virtual IDictionary Data
  54. {
  55. get
  56. {
  57. return _data ?? (_data = CreateDataContainer());
  58. }
  59. }
  60. private string GetClassName() => GetType().ToString();
  61. // Retrieves the lowest exception (inner most) for the given Exception.
  62. // This will traverse exceptions using the innerException property.
  63. public virtual Exception GetBaseException()
  64. {
  65. Exception? inner = InnerException;
  66. Exception back = this;
  67. while (inner != null)
  68. {
  69. back = inner;
  70. inner = inner.InnerException;
  71. }
  72. return back;
  73. }
  74. public Exception? InnerException => _innerException;
  75. // Sets the help link for this exception.
  76. // This should be in a URL/URN form, such as:
  77. // "file:///C:/Applications/Bazzal/help.html#ErrorNum42"
  78. public virtual string? HelpLink
  79. {
  80. get
  81. {
  82. return _helpURL;
  83. }
  84. set
  85. {
  86. _helpURL = value;
  87. }
  88. }
  89. public virtual string? Source
  90. {
  91. get
  92. {
  93. return _source ?? (_source = CreateSourceName());
  94. }
  95. set
  96. {
  97. _source = value;
  98. }
  99. }
  100. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  101. {
  102. if (info == null)
  103. {
  104. throw new ArgumentNullException(nameof(info));
  105. }
  106. if (_source == null)
  107. {
  108. _source = Source; // Set the Source information correctly before serialization
  109. }
  110. info.AddValue("ClassName", GetClassName(), typeof(string)); // Do not rename (binary serialization)
  111. info.AddValue("Message", _message, typeof(string)); // Do not rename (binary serialization)
  112. info.AddValue("Data", _data, typeof(IDictionary)); // Do not rename (binary serialization)
  113. info.AddValue("InnerException", _innerException, typeof(Exception)); // Do not rename (binary serialization)
  114. info.AddValue("HelpURL", _helpURL, typeof(string)); // Do not rename (binary serialization)
  115. info.AddValue("StackTraceString", SerializationStackTraceString, typeof(string)); // Do not rename (binary serialization)
  116. info.AddValue("RemoteStackTraceString", SerializationRemoteStackTraceString, typeof(string)); // Do not rename (binary serialization)
  117. info.AddValue("RemoteStackIndex", 0, typeof(int)); // Do not rename (binary serialization)
  118. info.AddValue("ExceptionMethod", null, typeof(string)); // Do not rename (binary serialization)
  119. info.AddValue("HResult", _HResult); // Do not rename (binary serialization)
  120. info.AddValue("Source", _source, typeof(string)); // Do not rename (binary serialization)
  121. info.AddValue("WatsonBuckets", SerializationWatsonBuckets, typeof(byte[])); // Do not rename (binary serialization)
  122. }
  123. public override string ToString()
  124. {
  125. string s = GetClassName();
  126. string? message = Message;
  127. if (!string.IsNullOrEmpty(message))
  128. {
  129. s += ": " + message;
  130. }
  131. if (_innerException != null)
  132. {
  133. s = s + Environment.NewLine + InnerExceptionPrefix + _innerException.ToString() + Environment.NewLine +
  134. " " + SR.Exception_EndOfInnerExceptionStack;
  135. }
  136. string? stackTrace = StackTrace;
  137. if (stackTrace != null)
  138. {
  139. s += Environment.NewLine + stackTrace;
  140. }
  141. return s;
  142. }
  143. protected event EventHandler<SafeSerializationEventArgs>? SerializeObjectState
  144. {
  145. add { throw new PlatformNotSupportedException(SR.PlatformNotSupported_SecureBinarySerialization); }
  146. remove { throw new PlatformNotSupportedException(SR.PlatformNotSupported_SecureBinarySerialization); }
  147. }
  148. public int HResult
  149. {
  150. get
  151. {
  152. return _HResult;
  153. }
  154. set
  155. {
  156. _HResult = value;
  157. }
  158. }
  159. // this method is required so Object.GetType is not made virtual by the compiler
  160. // _Exception.GetType()
  161. public new Type GetType() => base.GetType();
  162. partial void RestoreRemoteStackTrace(SerializationInfo info, StreamingContext context);
  163. }
  164. }