/sources/Core/Utilities/ExceptionUtilities.cs

https://github.com/terrafx/terrafx · C# · 949 lines · 561 code · 75 blank · 313 comment · 75 complexity · bf7d1cc5c87dd6086978fe64d4116f2e MD5 · raw file

  1. // Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using TerraFX.Runtime;
  9. using TerraFX.Threading;
  10. using static TerraFX.Utilities.MarshalUtilities;
  11. namespace TerraFX.Utilities
  12. {
  13. /// <summary>Provides a set of methods for throwing exceptions.</summary>
  14. public static unsafe class ExceptionUtilities
  15. {
  16. /// <summary>Throws an <see cref="ArgumentException" />.</summary>
  17. /// <param name="message">The message detailing the cause of the exception.</param>
  18. /// <param name="valueName">The name of the value that caused the exception.</param>
  19. /// <exception cref="ArgumentException"><paramref name="message" /></exception>
  20. [DoesNotReturn]
  21. public static void ThrowArgumentException(string message, string valueName)
  22. => throw new ArgumentException(message, valueName);
  23. /// <summary>Throws an <see cref="ArgumentNullException" />.</summary>
  24. /// <param name="valueName">The name of the value that is <c>null</c>.</param>
  25. /// <exception cref="InvalidOperationException"><paramref name="valueName" /> is <c>null</c>.</exception>
  26. [DoesNotReturn]
  27. public static void ThrowArgumentNullException(string valueName)
  28. {
  29. var message = string.Format(Resources.ValueIsNullMessage, valueName);
  30. throw new ArgumentNullException(valueName, message);
  31. }
  32. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" />.</summary>
  33. /// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
  34. /// <param name="message">The message detailing the cause of the exception.</param>
  35. /// <param name="value">The value that caused the exception.</param>
  36. /// <param name="valueName">The name of the value that caused the exception.</param>
  37. /// <exception cref="ArgumentOutOfRangeException"><paramref name="message" /></exception>
  38. [DoesNotReturn]
  39. public static void ThrowArgumentOutOfRangeException<T>(string message, T value, string valueName)
  40. => throw new ArgumentOutOfRangeException(valueName, value, message);
  41. /// <summary>Throws an <see cref="ExternalException" />.</summary>
  42. /// <param name="methodName">The name of the method that caused the exception.</param>
  43. /// <param name="errorCode">The underlying error code for the exception.</param>
  44. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="errorCode" />.</exception>
  45. [DoesNotReturn]
  46. public static void ThrowExternalException(string methodName, int errorCode)
  47. {
  48. var message = string.Format(Resources.UnmanagedMethodFailedMessage, methodName, errorCode);
  49. throw new ExternalException(message, errorCode);
  50. }
  51. /// <summary>Throws an <see cref="InvalidOperationException" /> for an empty queue.</summary>
  52. /// <exception cref="InvalidOperationException">The queue is empty.</exception>
  53. [DoesNotReturn]
  54. public static void ThrowForEmptyQueue()
  55. {
  56. var message = Resources.EmptyQueueMessage;
  57. ThrowInvalidOperationException(message);
  58. }
  59. /// <summary>Throws an <see cref="InvalidOperationException" /> for an empty stack.</summary>
  60. /// <exception cref="InvalidOperationException">The stack is empty.</exception>
  61. [DoesNotReturn]
  62. public static void ThrowForEmptyStack()
  63. {
  64. var message = Resources.EmptyStackMessage;
  65. ThrowInvalidOperationException(message);
  66. }
  67. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> for an invalid flags enum combination.</summary>
  68. /// <param name="value">The value that caused the exception.va</param>
  69. /// <param name="valueName">The name of the value that caused the exception.</param>
  70. /// <exception cref="ArgumentOutOfRangeException"><paramref name="valueName" /> has an invalid flag combination.</exception>
  71. [DoesNotReturn]
  72. public static void ThrowForInvalidFlagsCombination<TEnum>(TEnum value, string valueName)
  73. where TEnum : struct, Enum
  74. {
  75. var message = string.Format(Resources.InvalidFlagCombinationMessage, valueName);
  76. ThrowArgumentOutOfRangeException(message, value, valueName);
  77. }
  78. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> for an invalid enum kind.</summary>
  79. /// <typeparam name="TEnum">The type of <paramref name="value" />.</typeparam>
  80. /// <param name="value">The value that caused the exception.</param>
  81. /// <param name="valueName">The name of the value that caused the exception.</param>
  82. /// <exception cref="ArgumentOutOfRangeException">The kind for <paramref name="valueName" /> is unsupported.</exception>
  83. [DoesNotReturn]
  84. public static void ThrowForInvalidKind<TEnum>(TEnum value, string valueName)
  85. where TEnum : struct, Enum
  86. {
  87. var message = string.Format(Resources.InvalidKindMessage, valueName);
  88. ThrowArgumentOutOfRangeException(message, value, valueName);
  89. }
  90. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" />.</summary>
  91. /// <typeparam name="TEnum">The type of <paramref name="value" /> and <paramref name="expectedKind" />.</typeparam>
  92. /// <param name="value">The value that caused the exception.</param>
  93. /// <param name="valueName">The name of the value that caused the exception.</param>
  94. /// <param name="expectedKind">The expected kind of <paramref name="valueName" />.</param>
  95. /// <exception cref="ArgumentOutOfRangeException">The kind for <paramref name="valueName" /> is not <paramref name="expectedKind" />.</exception>
  96. [DoesNotReturn]
  97. public static void ThrowForInvalidKind<TEnum>(TEnum value, string valueName, TEnum expectedKind)
  98. where TEnum : struct, Enum
  99. {
  100. var message = string.Format(Resources.InvalidKindWithExpectedKindMessage, valueName, expectedKind.ToString());
  101. ThrowArgumentOutOfRangeException(message, value, valueName);
  102. }
  103. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> for an invalid parent.</summary>
  104. /// <param name="value">The value that caused the exception.</param>
  105. /// <param name="valueName">The name of the value that caused the exception.</param>
  106. /// <exception cref="ArgumentOutOfRangeException"><paramref name="valueName" /> is incompatible as it belongs to a different parent.</exception>
  107. [DoesNotReturn]
  108. public static void ThrowForInvalidParent<T>(T value, string valueName)
  109. {
  110. var message = string.Format(Resources.InvalidParentMessage, valueName);
  111. ThrowArgumentOutOfRangeException(message, value, valueName);
  112. }
  113. /// <summary>Throws an <see cref="InvalidOperationException" /> for an invalid state.</summary>
  114. /// <param name="expectedStateName">The name expected state.</param>
  115. /// <exception cref="ArgumentException">The current state is not <paramref name="expectedStateName" />.</exception>
  116. [DoesNotReturn]
  117. public static void ThrowForInvalidState(string expectedStateName)
  118. {
  119. var message = string.Format(Resources.InvalidStateMessage, expectedStateName);
  120. ThrowInvalidOperationException(message);
  121. }
  122. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> for an invalid type.</summary>
  123. /// <param name="type">The type that caused the exception.</param>
  124. /// <param name="valueName">The name of the value that is not <paramref name="expectedType" />.</param>
  125. /// <param name="expectedType">The expected type of the value.</param>
  126. /// <exception cref="ArgumentException"><paramref name="valueName" /> is not an instance of <paramref name="expectedType" />.</exception>
  127. [DoesNotReturn]
  128. public static void ThrowForInvalidType(Type type, string valueName, Type expectedType)
  129. {
  130. var message = string.Format(Resources.InvalidTypeMessage, valueName, expectedType);
  131. ThrowArgumentOutOfRangeException(message, type, valueName);
  132. }
  133. /// <summary>Throws an <see cref="ExternalException" /> using the last available error code.</summary>
  134. /// <param name="methodName">The name of the method that caused the exception.</param>
  135. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an exit code of <see cref="Marshal.GetLastWin32Error()" />.</exception>
  136. [DoesNotReturn]
  137. public static void ThrowForLastError(string methodName)
  138. => ThrowExternalException(methodName, GetLastError());
  139. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  140. /// <param name="result">The underlying error code for the exception.</param>
  141. /// <param name="methodName">The name of the method that caused the exception.</param>
  142. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  143. public static void ThrowForLastErrorIfNotZero(int result, string methodName)
  144. {
  145. if (result != 0)
  146. {
  147. ThrowExternalException(methodName, GetLastError());
  148. }
  149. }
  150. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  151. /// <param name="result">The underlying error code for the exception.</param>
  152. /// <param name="methodName">The name of the method that caused the exception.</param>
  153. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  154. public static void ThrowForLastErrorIfNotZero(long result, string methodName)
  155. {
  156. if (result != 0)
  157. {
  158. ThrowExternalException(methodName, GetLastError());
  159. }
  160. }
  161. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  162. /// <param name="result">The underlying error code for the exception.</param>
  163. /// <param name="methodName">The name of the method that caused the exception.</param>
  164. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  165. public static void ThrowForLastErrorIfNotZero(nint result, string methodName)
  166. {
  167. if (result != 0)
  168. {
  169. ThrowExternalException(methodName, GetLastError());
  170. }
  171. }
  172. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  173. /// <param name="result">The underlying error code for the exception.</param>
  174. /// <param name="methodName">The name of the method that caused the exception.</param>
  175. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  176. public static void ThrowForLastErrorIfNotZero(uint result, string methodName)
  177. {
  178. if (result != 0)
  179. {
  180. ThrowExternalException(methodName, GetLastError());
  181. }
  182. }
  183. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  184. /// <param name="result">The underlying error code for the exception.</param>
  185. /// <param name="methodName">The name of the method that caused the exception.</param>
  186. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  187. public static void ThrowForLastErrorIfNotZero(ulong result, string methodName)
  188. {
  189. if (result != 0)
  190. {
  191. ThrowExternalException(methodName, GetLastError());
  192. }
  193. }
  194. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is not <c>zero</c>.</summary>
  195. /// <param name="result">The underlying error code for the exception.</param>
  196. /// <param name="methodName">The name of the method that caused the exception.</param>
  197. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  198. public static void ThrowForLastErrorIfNotZero(nuint result, string methodName)
  199. {
  200. if (result != 0)
  201. {
  202. ThrowExternalException(methodName, GetLastError());
  203. }
  204. }
  205. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>null</c>.</summary>
  206. /// <param name="result">The underlying error code for the exception.</param>
  207. /// <param name="methodName">The name of the method that caused the exception.</param>
  208. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  209. public static void ThrowForLastErrorIfNull(void* result, string methodName)
  210. {
  211. if (result == null)
  212. {
  213. ThrowExternalException(methodName, GetLastError());
  214. }
  215. }
  216. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  217. /// <param name="result">The underlying error code for the exception.</param>
  218. /// <param name="methodName">The name of the method that caused the exception.</param>
  219. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  220. public static void ThrowForLastErrorIfZero(int result, string methodName)
  221. {
  222. if (result == 0)
  223. {
  224. ThrowExternalException(methodName, GetLastError());
  225. }
  226. }
  227. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  228. /// <param name="result">The underlying error code for the exception.</param>
  229. /// <param name="methodName">The name of the method that caused the exception.</param>
  230. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  231. public static void ThrowForLastErrorIfZero(long result, string methodName)
  232. {
  233. if (result == 0)
  234. {
  235. ThrowExternalException(methodName, GetLastError());
  236. }
  237. }
  238. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  239. /// <param name="result">The underlying error code for the exception.</param>
  240. /// <param name="methodName">The name of the method that caused the exception.</param>
  241. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  242. public static void ThrowForLastErrorIfZero(nint result, string methodName)
  243. {
  244. if (result == 0)
  245. {
  246. ThrowExternalException(methodName, GetLastError());
  247. }
  248. }
  249. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  250. /// <param name="result">The underlying error code for the exception.</param>
  251. /// <param name="methodName">The name of the method that caused the exception.</param>
  252. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  253. public static void ThrowForLastErrorIfZero(uint result, string methodName)
  254. {
  255. if (result == 0)
  256. {
  257. ThrowExternalException(methodName, GetLastError());
  258. }
  259. }
  260. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  261. /// <param name="result">The underlying error code for the exception.</param>
  262. /// <param name="methodName">The name of the method that caused the exception.</param>
  263. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  264. public static void ThrowForLastErrorIfZero(ulong result, string methodName)
  265. {
  266. if (result == 0)
  267. {
  268. ThrowExternalException(methodName, GetLastError());
  269. }
  270. }
  271. /// <summary>Throws an <see cref="ExternalException" /> if <paramref name="result" /> is <c>zero</c>.</summary>
  272. /// <param name="result">The underlying error code for the exception.</param>
  273. /// <param name="methodName">The name of the method that caused the exception.</param>
  274. /// <exception cref="ExternalException"><paramref name="methodName" /> failed with an error code of <paramref name="result" />.</exception>
  275. public static void ThrowForLastErrorIfZero(nuint result, string methodName)
  276. {
  277. if (result == 0)
  278. {
  279. ThrowExternalException(methodName, GetLastError());
  280. }
  281. }
  282. /// <summary>Throws a <see cref="NotSupportedException" />.</summary>
  283. /// <exception cref="NotSupportedException">One or more of the required features is not available</exception>
  284. [DoesNotReturn]
  285. public static void ThrowForMissingFeature()
  286. => throw new NotSupportedException(Resources.MissingRequiredFeaturesMessage);
  287. /// <summary>Throws a <see cref="NotSupportedException" />.</summary>
  288. /// <param name="surfaceName">The name of the surface that is not available.</param>
  289. /// <exception cref="NotSupportedException"><paramref name="surfaceName" /> is not a supported GraphicsSurfaceKind.</exception>
  290. [DoesNotReturn]
  291. public static void ThrowForUnsupportedSurfaceKind(string surfaceName)
  292. {
  293. var message = string.Format(Resources.UnsupportedSurfaceKindMessage, surfaceName);
  294. throw new NotSupportedException(message);
  295. }
  296. /// <summary>Throws a <see cref="ArgumentOutOfRangeException" /> for an unsupported type.</summary>
  297. /// <param name="type">The type that caused the exception.</param>
  298. /// <param name="valueName">The name of the value that is not a supported type.</param>
  299. /// <exception cref="ArgumentException"><paramref name="valueName" /> is the unsupported type <paramref name="type" />.</exception>
  300. [DoesNotReturn]
  301. public static void ThrowForUnsupportedType(Type type, string valueName)
  302. {
  303. var message = string.Format(Resources.UnsupportedTypeMessage, valueName, type);
  304. ThrowArgumentOutOfRangeException(message, type, valueName);
  305. }
  306. /// <summary>Throws an <see cref="ObjectDisposedException" /> if <paramref name="state" /> is <see cref="VolatileState.Disposed" /> or <see cref="VolatileState.Disposing" />.</summary>
  307. /// <param name="state">The state being checked.</param>
  308. /// <param name="valueName">The name of the value being checked.</param>
  309. /// <exception cref="ObjectDisposedException"><paramref name="valueName" /> is disposed or being disposed.</exception>
  310. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  311. public static void ThrowIfDisposedOrDisposing(VolatileState state, string valueName)
  312. {
  313. if (state.IsDisposedOrDisposing)
  314. {
  315. ThrowObjectDisposedException(valueName);
  316. }
  317. }
  318. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>negative</c>.</summary>
  319. /// <param name="value">The value to be checked if it is <c>negative</c>.</param>
  320. /// <param name="valueName">The name of the value being checked.</param>
  321. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>negative</c>.</exception>
  322. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  323. public static void ThrowIfNegative(int value, string valueName)
  324. {
  325. if (value < 0)
  326. {
  327. var message = string.Format(Resources.ValueIsNegativeMessage, valueName);
  328. ThrowArgumentOutOfRangeException(message, value, valueName);
  329. }
  330. }
  331. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>negative</c>.</summary>
  332. /// <param name="value">The value to be checked if it is <c>negative</c>.</param>
  333. /// <param name="valueName">The name of the value being checked.</param>
  334. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>negative</c>.</exception>
  335. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  336. public static void ThrowIfNegative(long value, string valueName)
  337. {
  338. if (value < 0)
  339. {
  340. var message = string.Format(Resources.ValueIsNegativeMessage, valueName);
  341. ThrowArgumentOutOfRangeException(message, value, valueName);
  342. }
  343. }
  344. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>negative</c>.</summary>
  345. /// <param name="value">The value to be checked if it is <c>negative</c>.</param>
  346. /// <param name="valueName">The name of the value being checked.</param>
  347. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>negative</c>.</exception>
  348. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  349. public static void ThrowIfNegative(nint value, string valueName)
  350. {
  351. if (value < 0)
  352. {
  353. var message = string.Format(Resources.ValueIsNegativeMessage, valueName);
  354. ThrowArgumentOutOfRangeException(message, value, valueName);
  355. }
  356. }
  357. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than or equal to <paramref name="length" />.</summary>
  358. /// <param name="index">The index to check.</param>
  359. /// <param name="length">The length of the collection being indexed.</param>
  360. /// <param name="indexName">The name of the index being checked.</param>
  361. /// <param name="lengthName">The name of the length being checked.</param>
  362. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than or equal to <paramref name="lengthName" />.</exception>
  363. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  364. public static void ThrowIfNotInBounds(int index, int length, string indexName, string lengthName)
  365. {
  366. if (unchecked((uint)index >= (uint)length))
  367. {
  368. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  369. ThrowArgumentOutOfRangeException(message, index, indexName);
  370. }
  371. }
  372. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than or equal to <paramref name="length" />.</summary>
  373. /// <param name="index">The index to check.</param>
  374. /// <param name="length">The length of the collection being indexed.</param>
  375. /// <param name="indexName">The name of the index being checked.</param>
  376. /// <param name="lengthName">The name of the length being checked.</param>
  377. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than or equal to <paramref name="lengthName" />.</exception>
  378. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  379. public static void ThrowIfNotInBounds(long index, long length, string indexName, string lengthName)
  380. {
  381. if (unchecked((ulong)index >= (ulong)length))
  382. {
  383. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  384. ThrowArgumentOutOfRangeException(message, index, indexName);
  385. }
  386. }
  387. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than or equal to <paramref name="length" />.</summary>
  388. /// <param name="index">The index to check.</param>
  389. /// <param name="length">The length of the collection being indexed.</param>
  390. /// <param name="indexName">The name of the index being checked.</param>
  391. /// <param name="lengthName">The name of the length being checked.</param>
  392. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than or equal to <paramref name="lengthName" />.</exception>
  393. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  394. public static void ThrowIfNotInBounds(nint index, nint length, string indexName, string lengthName)
  395. {
  396. if (unchecked((nuint)index >= (nuint)length))
  397. {
  398. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  399. ThrowArgumentOutOfRangeException(message, index, indexName);
  400. }
  401. }
  402. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than or equal to <paramref name="length" />.</summary>
  403. /// <param name="index">The index to check.</param>
  404. /// <param name="length">The length of the collection being indexed.</param>
  405. /// <param name="indexName">The name of the index being checked.</param>
  406. /// <param name="lengthName">The name of the length being checked.</param>
  407. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than or equal to <paramref name="lengthName" />.</exception>
  408. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  409. public static void ThrowIfNotInBounds(uint index, uint length, string indexName, string lengthName)
  410. {
  411. if (index >= length)
  412. {
  413. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  414. ThrowArgumentOutOfRangeException(message, index, indexName);
  415. }
  416. }
  417. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than or equal to <paramref name="length" />.</summary>
  418. /// <param name="index">The index to check.</param>
  419. /// <param name="length">The length of the collection being indexed.</param>
  420. /// <param name="indexName">The name of the index being checked.</param>
  421. /// <param name="lengthName">The name of the length being checked.</param>
  422. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than or equal to <paramref name="lengthName" />.</exception>
  423. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  424. public static void ThrowIfNotInBounds(ulong index, ulong length, string indexName, string lengthName)
  425. {
  426. if (index >= length)
  427. {
  428. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  429. ThrowArgumentOutOfRangeException(message, index, indexName);
  430. }
  431. }
  432. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than or equal to <paramref name="length" />.</summary>
  433. /// <param name="index">The index to check.</param>
  434. /// <param name="length">The length of the collection being indexed.</param>
  435. /// <param name="indexName">The name of the index being checked.</param>
  436. /// <param name="lengthName">The name of the length being checked.</param>
  437. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than or equal to <paramref name="lengthName" />.</exception>
  438. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  439. public static void ThrowIfNotInBounds(nuint index, nuint length, string indexName, string lengthName)
  440. {
  441. if (index >= length)
  442. {
  443. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  444. ThrowArgumentOutOfRangeException(message, index, indexName);
  445. }
  446. }
  447. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than <paramref name="length" />.</summary>
  448. /// <param name="index">The index to check.</param>
  449. /// <param name="length">The length of the collection being indexed.</param>
  450. /// <param name="indexName">The name of the index being checked.</param>
  451. /// <param name="lengthName">The name of the length being checked.</param>
  452. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than <paramref name="lengthName" />.</exception>
  453. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  454. public static void ThrowIfNotInInsertBounds(int index, int length, string indexName, string lengthName)
  455. {
  456. if (unchecked((uint)index > (uint)length))
  457. {
  458. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  459. ThrowArgumentOutOfRangeException(message, index, indexName);
  460. }
  461. }
  462. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than <paramref name="length" />.</summary>
  463. /// <param name="index">The index to check.</param>
  464. /// <param name="length">The length of the collection being indexed.</param>
  465. /// <param name="indexName">The name of the index being checked.</param>
  466. /// <param name="lengthName">The name of the length being checked.</param>
  467. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than <paramref name="lengthName" />.</exception>
  468. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  469. public static void ThrowIfNotInInsertBounds(long index, long length, string indexName, string lengthName)
  470. {
  471. if (unchecked((ulong)index > (ulong)length))
  472. {
  473. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  474. ThrowArgumentOutOfRangeException(message, index, indexName);
  475. }
  476. }
  477. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is <c>negative</c> or greater than <paramref name="length" />.</summary>
  478. /// <param name="index">The index to check.</param>
  479. /// <param name="length">The length of the collection being indexed.</param>
  480. /// <param name="indexName">The name of the index being checked.</param>
  481. /// <param name="lengthName">The name of the length being checked.</param>
  482. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is <c>negative</c> or greater than <paramref name="lengthName" />.</exception>
  483. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  484. public static void ThrowIfNotInInsertBounds(nint index, nint length, string indexName, string lengthName)
  485. {
  486. if (unchecked((nuint)index > (nuint)length))
  487. {
  488. var message = string.Format(Resources.ValueIsNotInSignedBoundsMessage, indexName, lengthName);
  489. ThrowArgumentOutOfRangeException(message, index, indexName);
  490. }
  491. }
  492. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than <paramref name="length" />.</summary>
  493. /// <param name="index">The index to check.</param>
  494. /// <param name="length">The length of the collection being indexed.</param>
  495. /// <param name="indexName">The name of the index being checked.</param>
  496. /// <param name="lengthName">The name of the length being checked.</param>
  497. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than <paramref name="lengthName" />.</exception>
  498. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  499. public static void ThrowIfNotInInsertBounds(uint index, uint length, string indexName, string lengthName)
  500. {
  501. if (index > length)
  502. {
  503. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  504. ThrowArgumentOutOfRangeException(message, index, indexName);
  505. }
  506. }
  507. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than <paramref name="length" />.</summary>
  508. /// <param name="index">The index to check.</param>
  509. /// <param name="length">The length of the collection being indexed.</param>
  510. /// <param name="indexName">The name of the index being checked.</param>
  511. /// <param name="lengthName">The name of the length being checked.</param>
  512. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than <paramref name="lengthName" />.</exception>
  513. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  514. public static void ThrowIfNotInInsertBounds(ulong index, ulong length, string indexName, string lengthName)
  515. {
  516. if (index > length)
  517. {
  518. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  519. ThrowArgumentOutOfRangeException(message, index, indexName);
  520. }
  521. }
  522. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="index" /> is greater than <paramref name="length" />.</summary>
  523. /// <param name="index">The index to check.</param>
  524. /// <param name="length">The length of the collection being indexed.</param>
  525. /// <param name="indexName">The name of the index being checked.</param>
  526. /// <param name="lengthName">The name of the length being checked.</param>
  527. /// <exception cref="ArgumentOutOfRangeException"><paramref name="indexName" /> is greater than <paramref name="lengthName" />.</exception>
  528. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  529. public static void ThrowIfNotInInsertBounds(nuint index, nuint length, string indexName, string lengthName)
  530. {
  531. if (index > length)
  532. {
  533. var message = string.Format(Resources.ValueIsNotInUnsignedBoundsMessage, indexName, lengthName);
  534. ThrowArgumentOutOfRangeException(message, index, indexName);
  535. }
  536. }
  537. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not a <c>power of two</c>.</summary>
  538. /// <param name="value">The value to check.</param>
  539. /// <param name="valueName">The name of <paramref name="value" />.</param>
  540. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not a <c>power of two</c>.</exception>
  541. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  542. public static void ThrowIfNotPow2(uint value, string valueName)
  543. {
  544. if (!MathUtilities.IsPow2(value))
  545. {
  546. var message = string.Format(Resources.ValueIsNotPow2Message, value);
  547. ThrowArgumentOutOfRangeException(message, value, valueName);
  548. }
  549. }
  550. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not a <c>power of two</c>.</summary>
  551. /// <param name="value">The value to check.</param>
  552. /// <param name="valueName">The name of <paramref name="value" />.</param>
  553. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not a <c>power of two</c>.</exception>
  554. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  555. public static void ThrowIfNotPow2(ulong value, string valueName)
  556. {
  557. if (!MathUtilities.IsPow2(value))
  558. {
  559. var message = string.Format(Resources.ValueIsNotPow2Message, value);
  560. ThrowArgumentOutOfRangeException(message, value, valueName);
  561. }
  562. }
  563. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not a <c>power of two</c>.</summary>
  564. /// <param name="value">The value to check.</param>
  565. /// <param name="valueName">The name of <paramref name="value" />.</param>
  566. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not a <c>power of two</c>.</exception>
  567. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  568. public static void ThrowIfNotPow2(nuint value, string valueName)
  569. {
  570. if (!MathUtilities.IsPow2(value))
  571. {
  572. var message = string.Format(Resources.ValueIsNotPow2Message, value);
  573. ThrowArgumentOutOfRangeException(message, value, valueName);
  574. }
  575. }
  576. /// <summary>Throws an <see cref="InvalidOperationException" /> if <see cref="Thread.CurrentThread" /> is not <paramref name="expectedThread" />.</summary>
  577. /// <param name="expectedThread">The thread to check that the code is running on.</param>
  578. /// <exception cref="InvalidOperationException">The current thread is not <paramref name="expectedThread" />.</exception>
  579. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  580. public static void ThrowIfNotThread(Thread expectedThread)
  581. {
  582. if (Thread.CurrentThread != expectedThread)
  583. {
  584. var message = string.Format(Resources.InvalidThreadMessage, expectedThread);
  585. ThrowInvalidOperationException(message);
  586. }
  587. }
  588. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  589. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  590. /// <param name="valueName">The name of the value being checked.</param>
  591. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  592. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  593. public static void ThrowIfNotZero(int value, string valueName)
  594. {
  595. if (value != 0)
  596. {
  597. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  598. ThrowArgumentOutOfRangeException(message, value, valueName);
  599. }
  600. }
  601. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  602. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  603. /// <param name="valueName">The name of the value being checked.</param>
  604. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  605. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  606. public static void ThrowIfNotZero(long value, string valueName)
  607. {
  608. if (value != 0)
  609. {
  610. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  611. ThrowArgumentOutOfRangeException(message, value, valueName);
  612. }
  613. }
  614. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  615. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  616. /// <param name="valueName">The name of the value being checked.</param>
  617. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  618. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  619. public static void ThrowIfNotZero(nint value, string valueName)
  620. {
  621. if (value != 0)
  622. {
  623. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  624. ThrowArgumentOutOfRangeException(message, value, valueName);
  625. }
  626. }
  627. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  628. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  629. /// <param name="valueName">The name of the value being checked.</param>
  630. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  631. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  632. public static void ThrowIfNotZero(uint value, string valueName)
  633. {
  634. if (value != 0)
  635. {
  636. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  637. ThrowArgumentOutOfRangeException(message, value, valueName);
  638. }
  639. }
  640. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  641. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  642. /// <param name="valueName">The name of the value being checked.</param>
  643. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  644. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  645. public static void ThrowIfNotZero(ulong value, string valueName)
  646. {
  647. if (value != 0)
  648. {
  649. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  650. ThrowArgumentOutOfRangeException(message, value, valueName);
  651. }
  652. }
  653. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is not <c>zero</c>.</summary>
  654. /// <param name="value">The value to be checked if it is not <c>zero</c>.</param>
  655. /// <param name="valueName">The name of the value being checked.</param>
  656. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is not <c>zero</c>.</exception>
  657. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  658. public static void ThrowIfNotZero(nuint value, string valueName)
  659. {
  660. if (value != 0)
  661. {
  662. var message = string.Format(Resources.ValueIsNotZeroMessage, valueName);
  663. ThrowArgumentOutOfRangeException(message, value, valueName);
  664. }
  665. }
  666. /// <summary>Throws an <see cref="ArgumentNullException" /> if <paramref name="value" /> is <c>null</c>.</summary>
  667. /// <typeparam name="T">The type of <paramref name="value" />.</typeparam>
  668. /// <param name="value">The value to be checked for <c>null</c>.</param>
  669. /// <param name="valueName">The name of the value being checked.</param>
  670. /// <exception cref="ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception>
  671. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  672. public static void ThrowIfNull<T>([NotNull] T? value, string valueName)
  673. where T : class
  674. {
  675. if (value is null)
  676. {
  677. ThrowArgumentNullException(valueName);
  678. }
  679. }
  680. /// <summary>Throws an <see cref="ArgumentNullException" /> if <paramref name="array" /> is <c>null</c>.</summary>
  681. /// <typeparam name="T">The type of items in <paramref name="array" />.</typeparam>
  682. /// <param name="array">The array to be checked for <c>null</c>.</param>
  683. /// <param name="arrayName">The name of the array being checked.</param>
  684. /// <exception cref="ArgumentNullException"><paramref name="array" /> is <c>null</c>.</exception>
  685. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  686. public static void ThrowIfNull<T>(UnmanagedArray<T> array, string arrayName)
  687. where T : unmanaged
  688. {
  689. if (array.IsNull)
  690. {
  691. ThrowArgumentNullException(arrayName);
  692. }
  693. }
  694. /// <summary>Throws a <see cref="ArgumentNullException" /> if <paramref name="value" /> is <c>null</c>.</summary>
  695. /// <param name="value">The value to be checked for <c>null</c>.</param>
  696. /// <param name="valueName">The name of the value being checked.</param>
  697. /// <exception cref="ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception>
  698. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  699. public static void ThrowIfNull(void* value, string valueName)
  700. {
  701. if (value == null)
  702. {
  703. ThrowArgumentNullException(valueName);
  704. }
  705. }
  706. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  707. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  708. /// <param name="valueName">The name of the value being checked.</param>
  709. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  710. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  711. public static void ThrowIfZero(int value, string valueName)
  712. {
  713. if (value == 0)
  714. {
  715. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  716. ThrowArgumentOutOfRangeException(message, value, valueName);
  717. }
  718. }
  719. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  720. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  721. /// <param name="valueName">The name of the value being checked.</param>
  722. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  723. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  724. public static void ThrowIfZero(long value, string valueName)
  725. {
  726. if (value == 0)
  727. {
  728. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  729. ThrowArgumentOutOfRangeException(message, value, valueName);
  730. }
  731. }
  732. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  733. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  734. /// <param name="valueName">The name of the value being checked.</param>
  735. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  736. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  737. public static void ThrowIfZero(nint value, string valueName)
  738. {
  739. if (value == 0)
  740. {
  741. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  742. ThrowArgumentOutOfRangeException(message, value, valueName);
  743. }
  744. }
  745. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  746. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  747. /// <param name="valueName">The name of the value being checked.</param>
  748. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  749. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  750. public static void ThrowIfZero(uint value, string valueName)
  751. {
  752. if (value == 0)
  753. {
  754. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  755. ThrowArgumentOutOfRangeException(message, value, valueName);
  756. }
  757. }
  758. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  759. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  760. /// <param name="valueName">The name of the value being checked.</param>
  761. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  762. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  763. public static void ThrowIfZero(ulong value, string valueName)
  764. {
  765. if (value == 0)
  766. {
  767. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  768. ThrowArgumentOutOfRangeException(message, value, valueName);
  769. }
  770. }
  771. /// <summary>Throws an <see cref="ArgumentOutOfRangeException" /> if <paramref name="value" /> is <c>zero</c>.</summary>
  772. /// <param name="value">The value to be checked if it is <c>zero</c>.</param>
  773. /// <param name="valueName">The name of the value being checked.</param>
  774. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is <c>zero</c>.</exception>
  775. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  776. public static void ThrowIfZero(nuint value, string valueName)
  777. {
  778. if (value == 0)
  779. {
  780. var message = string.Format(Resources.ValueIsZeroMessage, valueName);
  781. ThrowArgumentOutOfRangeException(message, value, valueName);
  782. }
  783. }
  784. /// <summary>Throws a <see cref="InvalidOperationException" />.</summary>
  785. /// <param name="message">The message detailing the cause of the exception.</param>
  786. /// <exception cref="InvalidOperationException"><paramref name="message" />.</exception>
  787. [DoesNotReturn]
  788. public static void ThrowInvalidOperationException(string message)
  789. => throw new InvalidOperationException(message);
  790. /// <summary>Throws a <see cref="KeyNotFoundException" />.</summary>
  791. /// <param name="key">The key that is missing from <paramref name="collectionName" />.</param>
  792. /// <param name="collectionName">The name of the collection which does not contain <paramref name="key" />.</param>
  793. /// <exception cref="KeyNotFoundException"><paramref name="key" /> is not a valid key to <paramref name="collectionName"/>.</exception>
  794. [DoesNotReturn]
  795. public static void ThrowKeyNotFoundException<TKey>(TKey key, string collectionName)
  796. where TKey : notnull
  797. {
  798. var message = string.Format(Resources.InvalidKeyMessage, key, collectionName);
  799. throw new KeyNotFoundException(message);
  800. }
  801. /// <summary>Throws a <see cref="NotImplementedException" />.</summary>
  802. /// <exception cref="NotImplementedException">The given code path is not currently implemented.</exception>
  803. [DoesNotReturn]
  804. public static void ThrowNotImplementedException()
  805. => throw new NotImplementedException(Resources.NotImplementedMessage);
  806. /// <summary>Throws an <see cref="ObjectDisposedException" />.</summary>
  807. /// <param name="valueName">The name of the value that is <see cref="VolatileState.Disposed" /> or <see cref="VolatileState.Disposing" />.</param>
  808. /// <exception cref="InvalidOperationException"><paramref name="valueName" /> is disposed or being disposed.</exception>
  809. [DoesNotReturn]
  810. public static void ThrowObjectDisposedException(string valueName)
  811. {
  812. var message = string.Format(Resources.ObjectDisposedOrDisposingMessage, valueName);
  813. throw new ObjectDisposedException(message);
  814. }
  815. /// <summary>Throws an <see cref="OutOfMemoryException" />.</summary>
  816. /// <param name="size">The size, in bytes, of the failed allocation.</param>
  817. /// <exception cref="OutOfMemoryException">The allocation of <paramref name="size" /> bytes failed.</exception>
  818. [DoesNotReturn]
  819. public static void ThrowOutOfMemoryException(ulong size)
  820. {
  821. var message = string.Format(Resources.AllocationFailedMessage, size);
  822. throw new OutOfMemoryException(message);
  823. }
  824. /// <summary>Throws an <see cref="OutOfMemoryException" />.</summary>
  825. /// <param name="size">The size, in bytes, of the failed allocation.</param>
  826. /// <exception cref="OutOfMemoryException">The allocation of <paramref name="size" /> bytes failed.</exception>
  827. [DoesNotReturn]
  828. public static void ThrowOutOfMemoryException(nuint size)
  829. {
  830. var message = string.Format(Resources.AllocationFailedMessage, size);
  831. throw new OutOfMemoryException(message);
  832. }
  833. /// <summary>Throws an <see cref="OutOfMemoryException" />.</summary>
  834. /// <param name="count">The count, in elements, of the failed allocation.</param>
  835. /// <param name="size">The size, in bytes, of the elements in the failed allocation.</param>
  836. /// <exception cref="OutOfMemoryException">The allocation of <paramref name="size" /> bytes failed.</exception>
  837. [DoesNotReturn]
  838. public static void ThrowOutOfMemoryException(ulong count, ulong size)
  839. {
  840. var message = string.Format(Resources.ArrayAllocationFailedMessage, count, size);
  841. throw new OutOfMemoryException(message);
  842. }
  843. /// <summary>Throws an <see cref="OutOfMemoryException" />.</summary>
  844. /// <param name="count">The count, in elements, of the failed allocation.</param>
  845. /// <param name="size">The size, in bytes, of the elements in the failed allocation.</param>
  846. /// <exception cref="OutOfMemoryException">The allocation of <paramref name="size" /> bytes failed.</exception>
  847. [DoesNotReturn]
  848. public static void ThrowOutOfMemoryException(nuint count, nuint size)
  849. {
  850. var message = string.Format(Resources.ArrayAllocationFailedMessage, count, size);
  851. throw new OutOfMemoryException(message);
  852. }
  853. /// <summary>Throws a <see cref="TimeoutException" />.</summary>
  854. /// <param name="methodName">The name of the method that failed to complete within <paramref name="millisecondsTimeout" />.</param>
  855. /// <param name="millisecondsTimeout">The timeout, in milliseconds, for <paramref name="methodName"/>.</param>
  856. /// <exception cref="TimeoutException"><paramref name="methodName" /> failed to complete within <paramref name="millisecondsTimeout" /> ms.</exception>
  857. [DoesNotReturn]
  858. public static void ThrowTimeoutException(string methodName, int millisecondsTimeout)
  859. {
  860. var message = string.Format(Resources.MethodTimeoutMessage, methodName, millisecondsTimeout);
  861. throw new TimeoutException(message);
  862. }
  863. /// <summary>Throws a <see cref="TimeoutException" />.</summary>
  864. /// <param name="methodName">The name of the method that failed to complete within <paramref name="timeout" />.</param>
  865. /// <param name="timeout">The timeout for <paramref name="methodName"/>.</param>
  866. /// <exception cref="TimeoutException"><paramref name="methodName" /> failed to complete within <paramref name="timeout" /> ms.</exception>
  867. [DoesNotReturn]
  868. public static void ThrowTimeoutException(string methodName, TimeSpan timeout)
  869. {
  870. var message = string.Format(Resources.MethodTimeoutMessage, methodName, timeout.TotalMilliseconds);
  871. throw new TimeoutException(message);
  872. }
  873. }
  874. }