Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Exception.cs
Go to the documentation of this file.
8using System.Text;
9
10namespace System;
11
13[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15{
17 {
18 ThreadAbort = 1,
21 }
22
23 internal readonly struct DispatchState
24 {
25 public readonly byte[] StackTrace;
26
27 public readonly object[] DynamicMethods;
28
29 public readonly string RemoteStackTrace;
30
31 public readonly UIntPtr IpForWatsonBuckets;
32
33 public readonly byte[] WatsonBuckets;
34
43 }
44
46
47 internal string _message;
48
50
51 private readonly Exception _innerException;
52
53 private string _helpURL;
54
55 private byte[] _stackTrace;
56
57 private byte[] _watsonBuckets;
58
59 private string _stackTraceString;
60
62
63 private readonly object[] _dynamicMethods;
64
65 private string _source;
66
68
69 private readonly IntPtr _xptrs;
70
71 private readonly int _xcode = -532462766;
72
73 private int _HResult;
74
75 private const int _COMPlusExceptionCode = -532462766;
76
77 private protected const string InnerExceptionPrefix = " ---> ";
78
80 {
81 get
82 {
83 if (_exceptionMethod != null)
84 {
85 return _exceptionMethod;
86 }
87 if (_stackTrace == null)
88 {
89 return null;
90 }
92 return _exceptionMethod;
93 }
94 }
95
96 private bool HasBeenThrown => _stackTrace != null;
97
99
100 public virtual string Message => _message ?? SR.Format(SR.Exception_WasThrown, GetClassName());
101
102 public virtual IDictionary Data => _data ?? (_data = CreateDataContainer());
103
105
106 public virtual string? HelpLink
107 {
108 get
109 {
110 return _helpURL;
111 }
112 set
113 {
114 _helpURL = value;
115 }
116 }
117
118 public virtual string? Source
119 {
120 get
121 {
122 return _source ?? (_source = CreateSourceName());
123 }
124 set
125 {
126 _source = value;
127 }
128 }
129
130 public int HResult
131 {
132 get
133 {
134 return _HResult;
135 }
136 set
137 {
138 _HResult = value;
139 }
140 }
141
142 public virtual string? StackTrace
143 {
144 get
145 {
148 if (stackTraceString != null)
149 {
151 }
152 if (!HasBeenThrown)
153 {
155 }
157 }
158 }
159
161 {
162 get
163 {
164 string text = _stackTraceString;
165 if (text == null && HasBeenThrown)
166 {
168 }
169 return text;
170 }
171 }
172
173 [Obsolete("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
174 protected event EventHandler<SafeSerializationEventArgs>? SerializeObjectState
175 {
176 add
177 {
179 }
180 remove
181 {
183 }
184 }
185
187 {
189 {
191 }
192 return new ListDictionaryInternal();
193 }
194
195 [MethodImpl(MethodImplOptions.InternalCall)]
196 private static extern bool IsImmutableAgileException(Exception e);
197
198 [MethodImpl(MethodImplOptions.InternalCall)]
200
210
211 private string CreateSourceName()
212 {
213 StackTrace stackTrace = new StackTrace(this, fNeedFileInfo: false);
214 if (stackTrace.FrameCount > 0)
215 {
216 StackFrame frame = stackTrace.GetFrame(0);
217 MethodBase method = frame.GetMethod();
218 Module module = method.Module;
220 if ((object)runtimeModule == null)
221 {
223 {
225 }
226 runtimeModule = moduleBuilder.InternalModule;
227 }
228 return runtimeModule.GetRuntimeAssembly().GetSimpleName();
229 }
230 return null;
231 }
232
234 private void OnDeserialized(StreamingContext context)
235 {
236 _stackTrace = null;
238 }
239
241 {
242 _ = Source;
243 string stackTrace = StackTrace;
244 if (!string.IsNullOrEmpty(stackTrace))
245 {
247 }
248 _stackTrace = null;
249 _stackTraceString = null;
250 }
251
252 [MethodImpl(MethodImplOptions.InternalCall)]
253 private static extern void PrepareForForeignExceptionRaise();
254
255 [MethodImpl(MethodImplOptions.InternalCall)]
257
258 [MethodImpl(MethodImplOptions.InternalCall)]
260
261 [MethodImpl(MethodImplOptions.InternalCall)]
262 internal static extern uint GetExceptionCount();
263
265 {
266 if (!IsImmutableAgileException(this))
267 {
268 byte[] currentStackTrace = (byte[])dispatchState.StackTrace?.Clone();
269 object[] dynamicMethodArray = (object[])dispatchState.DynamicMethods?.Clone();
270 _watsonBuckets = dispatchState.WatsonBuckets;
271 _ipForWatsonBuckets = dispatchState.IpForWatsonBuckets;
272 _remoteStackTraceString = dispatchState.RemoteStackTrace;
274 _stackTraceString = null;
276 }
277 }
278
280 {
281 string s = null;
283 return s;
284 }
285
286 [DllImport("QCall", CharSet = CharSet.Unicode)]
288
294
296 {
298 {
299 return false;
300 }
301 if (_stackTrace != null || _stackTraceString != null || _remoteStackTraceString != null)
302 {
304 }
305 return true;
306 }
307
308 public Exception()
309 {
310 _HResult = -2146233088;
311 }
312
313 public Exception(string? message)
314 : this()
315 {
316 _message = message;
317 }
318
319 public Exception(string? message, Exception? innerException)
320 : this()
321 {
322 _message = message;
324 }
325
327 {
328 if (info == null)
329 {
330 throw new ArgumentNullException("info");
331 }
332 _message = info.GetString("Message");
333 _data = (IDictionary)info.GetValueNoThrow("Data", typeof(IDictionary));
334 _innerException = (Exception)info.GetValue("InnerException", typeof(Exception));
335 _helpURL = info.GetString("HelpURL");
336 _stackTraceString = info.GetString("StackTraceString");
337 _remoteStackTraceString = info.GetString("RemoteStackTraceString");
338 _HResult = info.GetInt32("HResult");
339 _source = info.GetString("Source");
341 }
342
343 private string GetClassName()
344 {
345 return GetType().ToString();
346 }
347
349 {
351 Exception result = this;
352 while (innerException != null)
353 {
354 result = innerException;
356 }
357 return result;
358 }
359
361 {
362 if (info == null)
363 {
364 throw new ArgumentNullException("info");
365 }
366 if (_source == null)
367 {
368 _source = Source;
369 }
370 info.AddValue("ClassName", GetClassName(), typeof(string));
371 info.AddValue("Message", _message, typeof(string));
372 info.AddValue("Data", _data, typeof(IDictionary));
373 info.AddValue("InnerException", _innerException, typeof(Exception));
374 info.AddValue("HelpURL", _helpURL, typeof(string));
375 info.AddValue("StackTraceString", SerializationStackTraceString, typeof(string));
376 info.AddValue("RemoteStackTraceString", _remoteStackTraceString, typeof(string));
377 info.AddValue("RemoteStackIndex", 0, typeof(int));
378 info.AddValue("ExceptionMethod", null, typeof(string));
379 info.AddValue("HResult", _HResult);
380 info.AddValue("Source", _source, typeof(string));
381 info.AddValue("WatsonBuckets", SerializationWatsonBuckets, typeof(byte[]));
382 }
383
384 public override string ToString()
385 {
386 string className = GetClassName();
387 string message = Message;
388 string text = _innerException?.ToString() ?? "";
390 string stackTrace = StackTrace;
391 int num = className.Length;
392 checked
393 {
394 if (!string.IsNullOrEmpty(message))
395 {
396 num += 2 + message.Length;
397 }
398 if (_innerException != null)
399 {
400 num += "\r\n".Length + " ---> ".Length + text.Length + "\r\n".Length + 3 + exception_EndOfInnerExceptionStack.Length;
401 }
402 if (stackTrace != null)
403 {
404 num += "\r\n".Length + stackTrace.Length;
405 }
406 string text2 = string.FastAllocateString(num);
407 Span<char> dest2 = new Span<char>(ref text2.GetRawStringData(), text2.Length);
408 Write(className, ref dest2);
409 if (!string.IsNullOrEmpty(message))
410 {
411 Write(": ", ref dest2);
412 Write(message, ref dest2);
413 }
414 if (_innerException != null)
415 {
416 Write("\r\n", ref dest2);
417 Write(" ---> ", ref dest2);
418 Write(text, ref dest2);
419 Write("\r\n", ref dest2);
420 Write(" ", ref dest2);
422 }
423 if (stackTrace != null)
424 {
425 Write("\r\n", ref dest2);
426 Write(stackTrace, ref dest2);
427 }
428 return text2;
429 }
430 static void Write(string source, ref Span<char> dest)
431 {
432 source.CopyTo(dest);
433 dest = dest.Slice(source.Length);
434 }
435 }
436
437 public new Type GetType()
438 {
439 return base.GetType();
440 }
441
443 {
444 _watsonBuckets = (byte[])info.GetValueNoThrow("WatsonBuckets", typeof(byte[]));
445 if (context.State == StreamingContextStates.CrossAppDomain)
446 {
448 _stackTraceString = null;
449 }
450 }
451
452 private string GetStackTrace()
453 {
455 }
456
468
469 internal void SetRemoteStackTrace(string stackTrace)
470 {
472 {
474 }
475 }
476}
virtual ? MethodBase GetMethod()
static void GetMessageFromNativeResources(ExceptionMessageKind kind, StringHandleOnStack retMesg)
string CreateSourceName()
Definition Exception.cs:211
string? SerializationStackTraceString
Definition Exception.cs:161
void RestoreDispatchState(in DispatchState dispatchState)
Definition Exception.cs:264
override string ToString()
Definition Exception.cs:384
IDictionary _data
Definition Exception.cs:49
Exception(SerializationInfo info, StreamingContext context)
Definition Exception.cs:326
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Definition Exception.cs:360
virtual ? string Source
Definition Exception.cs:119
void SetCurrentStackTrace()
Definition Exception.cs:458
MethodBase GetExceptionMethodFromStackTrace()
Definition Exception.cs:201
MethodBase _exceptionMethod
Definition Exception.cs:45
readonly IntPtr _xptrs
Definition Exception.cs:69
const string InnerExceptionPrefix
Definition Exception.cs:77
byte[] _stackTrace
Definition Exception.cs:55
Exception(string? message)
Definition Exception.cs:313
string _stackTraceString
Definition Exception.cs:59
Exception(string? message, Exception? innerException)
Definition Exception.cs:319
void SetRemoteStackTrace(string stackTrace)
Definition Exception.cs:469
virtual ? string StackTrace
Definition Exception.cs:143
object? SerializationWatsonBuckets
Definition Exception.cs:98
string GetStackTrace()
Definition Exception.cs:452
virtual Exception GetBaseException()
Definition Exception.cs:348
byte[] _watsonBuckets
Definition Exception.cs:57
UIntPtr _ipForWatsonBuckets
Definition Exception.cs:67
static uint GetExceptionCount()
DispatchState CaptureDispatchState()
Definition Exception.cs:289
readonly int _xcode
Definition Exception.cs:71
readonly Exception _innerException
Definition Exception.cs:51
virtual string Message
Definition Exception.cs:100
EventHandler< SafeSerializationEventArgs >? SerializeObjectState
Definition Exception.cs:175
const int _COMPlusExceptionCode
Definition Exception.cs:75
void OnDeserialized(StreamingContext context)
Definition Exception.cs:234
readonly object[] _dynamicMethods
Definition Exception.cs:63
MethodBase? TargetSite
Definition Exception.cs:80
IDictionary CreateDataContainer()
Definition Exception.cs:186
bool CanSetRemoteStackTrace()
Definition Exception.cs:295
new Type GetType()
Definition Exception.cs:437
static void PrepareForForeignExceptionRaise()
string GetClassName()
Definition Exception.cs:343
string _remoteStackTraceString
Definition Exception.cs:61
Exception? InnerException
Definition Exception.cs:104
virtual ? string HelpLink
Definition Exception.cs:107
static string GetMessageFromNativeResources(ExceptionMessageKind kind)
Definition Exception.cs:279
static bool IsImmutableAgileException(Exception e)
static void SaveStackTracesFromDeepCopy(Exception exception, byte[] currentStackTrace, object[] dynamicMethodArray)
static void GetStackTracesDeepCopy(Exception exception, out byte[] currentStackTrace, out object[] dynamicMethodArray)
void RestoreRemoteStackTrace(SerializationInfo info, StreamingContext context)
Definition Exception.cs:442
static IRuntimeMethodInfo GetMethodFromStackTrace(object stackTrace)
void InternalPreserveStackTrace()
Definition Exception.cs:240
static MethodBase GetMethodBase(RuntimeModule scope, int typeMetadataToken)
static string Exception_WasThrown
Definition SR.cs:1310
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_MustBeRuntimeReflectionObject
Definition SR.cs:780
static string Exception_EndOfInnerExceptionStack
Definition SR.cs:1306
static string PlatformNotSupported_SecureBinarySerialization
Definition SR.cs:1796
static string Exception_EndStackTraceFromPreviousThrow
Definition SR.cs:1308
Definition SR.cs:7
static void ThrowInvalidOperationException()
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
override string ToString()
Definition Type.cs:1108
DispatchState(byte[] stackTrace, object[] dynamicMethods, string remoteStackTrace, UIntPtr ipForWatsonBuckets, byte[] watsonBuckets)
Definition Exception.cs:35
readonly UIntPtr IpForWatsonBuckets
Definition Exception.cs:31
readonly byte[] WatsonBuckets
Definition Exception.cs:33
readonly string RemoteStackTrace
Definition Exception.cs:29
readonly object[] DynamicMethods
Definition Exception.cs:27
static readonly UIntPtr Zero
Definition UIntPtr.cs:19