Skip to content

Commit e3bac99

Browse files
author
msftbot[bot]
authored
Parameterless ThrowHelper APIs (#3550)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> ## Follow up for #3346 and #3455 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> <!-- - Bugfix --> - Feature <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> The `ThrowHelper` class exposes static throw-helper APIs that mirror the existing constructor for all the supported exception types. In the previous PRs though I had missed the parameterless constructors, so there were no APIs for that in the `ThrowHelper` class. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> `ThrowHelper` now exposes a parameterless overload for all the supported exception types that are exposed. ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes
2 parents fdee563 + 7d7451c commit e3bac99

File tree

2 files changed

+462
-0
lines changed

2 files changed

+462
-0
lines changed

Microsoft.Toolkit/Diagnostics/ThrowHelper.Generic.cs

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ namespace Microsoft.Toolkit.Diagnostics
2020
/// </summary>
2121
public static partial class ThrowHelper
2222
{
23+
/// <summary>
24+
/// Throws a new <see cref="ArrayTypeMismatchException"/>.
25+
/// </summary>
26+
/// <typeparam name="T">The type of expected result.</typeparam>
27+
/// <exception cref="ArrayTypeMismatchException">Thrown with no parameters.</exception>
28+
/// <returns>This method always throws, so it actually never returns a value.</returns>
29+
[DoesNotReturn]
30+
public static T ThrowArrayTypeMismatchException<T>()
31+
{
32+
throw new ArrayTypeMismatchException();
33+
}
34+
2335
/// <summary>
2436
/// Throws a new <see cref="ArrayTypeMismatchException"/>.
2537
/// </summary>
@@ -47,6 +59,18 @@ public static T ThrowArrayTypeMismatchException<T>(string message, Exception inn
4759
throw new ArrayTypeMismatchException(message, innerException);
4860
}
4961

62+
/// <summary>
63+
/// Throws a new <see cref="ArgumentException"/>.
64+
/// </summary>
65+
/// <typeparam name="T">The type of expected result.</typeparam>
66+
/// <exception cref="ArgumentException">Thrown with no parameters.</exception>
67+
/// <returns>This method always throws, so it actually never returns a value.</returns>
68+
[DoesNotReturn]
69+
public static T ThrowArgumentException<T>()
70+
{
71+
throw new ArgumentException();
72+
}
73+
5074
/// <summary>
5175
/// Throws a new <see cref="ArgumentException"/>.
5276
/// </summary>
@@ -103,6 +127,18 @@ public static T ThrowArgumentException<T>(string name, string message, Exception
103127
throw new ArgumentException(message, name, innerException);
104128
}
105129

130+
/// <summary>
131+
/// Throws a new <see cref="ArgumentNullException"/>.
132+
/// </summary>
133+
/// <typeparam name="T">The type of expected result.</typeparam>
134+
/// <exception cref="ArgumentNullException">Thrown with no parameters.</exception>
135+
/// <returns>This method always throws, so it actually never returns a value.</returns>
136+
[DoesNotReturn]
137+
public static T ThrowArgumentNullException<T>()
138+
{
139+
throw new ArgumentNullException();
140+
}
141+
106142
/// <summary>
107143
/// Throws a new <see cref="ArgumentNullException"/>.
108144
/// </summary>
@@ -144,6 +180,18 @@ public static T ThrowArgumentNullException<T>(string name, string message)
144180
throw new ArgumentNullException(name, message);
145181
}
146182

183+
/// <summary>
184+
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
185+
/// </summary>
186+
/// <typeparam name="T">The type of expected result.</typeparam>
187+
/// <exception cref="ArgumentOutOfRangeException">Thrown with no parameters.</exception>
188+
/// <returns>This method always throws, so it actually never returns a value.</returns>
189+
[DoesNotReturn]
190+
public static T ThrowArgumentOutOfRangeException<T>()
191+
{
192+
throw new ArgumentOutOfRangeException();
193+
}
194+
147195
/// <summary>
148196
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
149197
/// </summary>
@@ -201,6 +249,18 @@ public static T ThrowArgumentOutOfRangeException<T>(string name, object value, s
201249
}
202250

203251
#if !NETSTANDARD1_4
252+
/// <summary>
253+
/// Throws a new <see cref="COMException"/>.
254+
/// </summary>
255+
/// <typeparam name="T">The type of expected result.</typeparam>
256+
/// <exception cref="COMException">Thrown with no parameters.</exception>
257+
/// <returns>This method always throws, so it actually never returns a value.</returns>
258+
[DoesNotReturn]
259+
public static T ThrowCOMException<T>()
260+
{
261+
throw new COMException();
262+
}
263+
204264
/// <summary>
205265
/// Throws a new <see cref="COMException"/>.
206266
/// </summary>
@@ -242,6 +302,18 @@ public static T ThrowCOMException<T>(string message, int error)
242302
throw new COMException(message, error);
243303
}
244304

305+
/// <summary>
306+
/// Throws a new <see cref="ExternalException"/>.
307+
/// </summary>
308+
/// <typeparam name="T">The type of expected result.</typeparam>
309+
/// <exception cref="ExternalException">Thrown with no parameters.</exception>
310+
/// <returns>This method always throws, so it actually never returns a value.</returns>
311+
[DoesNotReturn]
312+
public static T ThrowExternalException<T>()
313+
{
314+
throw new ExternalException();
315+
}
316+
245317
/// <summary>
246318
/// Throws a new <see cref="ExternalException"/>.
247319
/// </summary>
@@ -284,6 +356,18 @@ public static T ThrowExternalException<T>(string message, int error)
284356
}
285357
#endif
286358

359+
/// <summary>
360+
/// Throws a new <see cref="FormatException"/>.
361+
/// </summary>
362+
/// <typeparam name="T">The type of expected result.</typeparam>
363+
/// <exception cref="FormatException">Thrown with no parameters.</exception>
364+
/// <returns>This method always throws, so it actually never returns a value.</returns>
365+
[DoesNotReturn]
366+
public static T ThrowFormatException<T>()
367+
{
368+
throw new FormatException();
369+
}
370+
287371
/// <summary>
288372
/// Throws a new <see cref="FormatException"/>.
289373
/// </summary>
@@ -312,6 +396,18 @@ public static T ThrowFormatException<T>(string message, Exception innerException
312396
}
313397

314398
#if !NETSTANDARD1_4
399+
/// <summary>
400+
/// Throws a new <see cref="InsufficientMemoryException"/>.
401+
/// </summary>
402+
/// <typeparam name="T">The type of expected result.</typeparam>
403+
/// <exception cref="InsufficientMemoryException">Thrown with no parameters.</exception>
404+
/// <returns>This method always throws, so it actually never returns a value.</returns>
405+
[DoesNotReturn]
406+
public static T ThrowInsufficientMemoryException<T>()
407+
{
408+
throw new InsufficientMemoryException();
409+
}
410+
315411
/// <summary>
316412
/// Throws a new <see cref="InsufficientMemoryException"/>.
317413
/// </summary>
@@ -340,6 +436,18 @@ public static T ThrowInsufficientMemoryException<T>(string message, Exception in
340436
}
341437
#endif
342438

439+
/// <summary>
440+
/// Throws a new <see cref="InvalidDataException"/>.
441+
/// </summary>
442+
/// <typeparam name="T">The type of expected result.</typeparam>
443+
/// <exception cref="InvalidDataException">Thrown with no parameters.</exception>
444+
/// <returns>This method always throws, so it actually never returns a value.</returns>
445+
[DoesNotReturn]
446+
public static T ThrowInvalidDataException<T>()
447+
{
448+
throw new InvalidDataException();
449+
}
450+
343451
/// <summary>
344452
/// Throws a new <see cref="InvalidDataException"/>.
345453
/// </summary>
@@ -367,6 +475,18 @@ public static T ThrowInvalidDataException<T>(string message, Exception innerExce
367475
throw new InvalidDataException(message, innerException);
368476
}
369477

478+
/// <summary>
479+
/// Throws a new <see cref="InvalidOperationException"/>.
480+
/// </summary>
481+
/// <typeparam name="T">The type of expected result.</typeparam>
482+
/// <exception cref="InvalidOperationException">Thrown with no parameters.</exception>
483+
/// <returns>This method always throws, so it actually never returns a value.</returns>
484+
[DoesNotReturn]
485+
public static T ThrowInvalidOperationException<T>()
486+
{
487+
throw new InvalidOperationException();
488+
}
489+
370490
/// <summary>
371491
/// Throws a new <see cref="InvalidOperationException"/>.
372492
/// </summary>
@@ -394,6 +514,18 @@ public static T ThrowInvalidOperationException<T>(string message, Exception inne
394514
throw new InvalidOperationException(message, innerException);
395515
}
396516

517+
/// <summary>
518+
/// Throws a new <see cref="LockRecursionException"/>.
519+
/// </summary>
520+
/// <typeparam name="T">The type of expected result.</typeparam>
521+
/// <exception cref="LockRecursionException">Thrown with no parameters.</exception>
522+
/// <returns>This method always throws, so it actually never returns a value.</returns>
523+
[DoesNotReturn]
524+
public static T ThrowLockRecursionException<T>()
525+
{
526+
throw new LockRecursionException();
527+
}
528+
397529
/// <summary>
398530
/// Throws a new <see cref="LockRecursionException"/>.
399531
/// </summary>
@@ -421,6 +553,18 @@ public static T ThrowLockRecursionException<T>(string message, Exception innerEx
421553
throw new LockRecursionException(message, innerException);
422554
}
423555

556+
/// <summary>
557+
/// Throws a new <see cref="MissingFieldException"/>.
558+
/// </summary>
559+
/// <typeparam name="T">The type of expected result.</typeparam>
560+
/// <exception cref="MissingFieldException">Thrown with no parameters.</exception>
561+
/// <returns>This method always throws, so it actually never returns a value.</returns>
562+
[DoesNotReturn]
563+
public static T ThrowMissingFieldException<T>()
564+
{
565+
throw new MissingFieldException();
566+
}
567+
424568
/// <summary>
425569
/// Throws a new <see cref="MissingFieldException"/>.
426570
/// </summary>
@@ -464,6 +608,18 @@ public static T ThrowMissingFieldException<T>(string className, string fieldName
464608
}
465609
#endif
466610

611+
/// <summary>
612+
/// Throws a new <see cref="MissingMemberException"/>.
613+
/// </summary>
614+
/// <typeparam name="T">The type of expected result.</typeparam>
615+
/// <exception cref="MissingMemberException">Thrown with no parameters.</exception>
616+
/// <returns>This method always throws, so it actually never returns a value.</returns>
617+
[DoesNotReturn]
618+
public static T ThrowMissingMemberException<T>()
619+
{
620+
throw new MissingMemberException();
621+
}
622+
467623
/// <summary>
468624
/// Throws a new <see cref="MissingMemberException"/>.
469625
/// </summary>
@@ -507,6 +663,18 @@ public static T ThrowMissingMemberException<T>(string className, string memberNa
507663
}
508664
#endif
509665

666+
/// <summary>
667+
/// Throws a new <see cref="MissingMethodException"/>.
668+
/// </summary>
669+
/// <typeparam name="T">The type of expected result.</typeparam>
670+
/// <exception cref="MissingMethodException">Thrown with no parameters.</exception>
671+
/// <returns>This method always throws, so it actually never returns a value.</returns>
672+
[DoesNotReturn]
673+
public static T ThrowMissingMethodException<T>()
674+
{
675+
throw new MissingMethodException();
676+
}
677+
510678
/// <summary>
511679
/// Throws a new <see cref="MissingMethodException"/>.
512680
/// </summary>
@@ -550,6 +718,18 @@ public static T ThrowMissingMethodException<T>(string className, string methodNa
550718
}
551719
#endif
552720

721+
/// <summary>
722+
/// Throws a new <see cref="NotSupportedException"/>.
723+
/// </summary>
724+
/// <typeparam name="T">The type of expected result.</typeparam>
725+
/// <exception cref="NotSupportedException">Thrown with no parameters.</exception>
726+
/// <returns>This method always throws, so it actually never returns a value.</returns>
727+
[DoesNotReturn]
728+
public static T ThrowNotSupportedException<T>()
729+
{
730+
throw new NotSupportedException();
731+
}
732+
553733
/// <summary>
554734
/// Throws a new <see cref="NotSupportedException"/>.
555735
/// </summary>
@@ -618,6 +798,18 @@ public static T ThrowObjectDisposedException<T>(string objectName, string messag
618798
throw new ObjectDisposedException(objectName, message);
619799
}
620800

801+
/// <summary>
802+
/// Throws a new <see cref="OperationCanceledException"/>.
803+
/// </summary>
804+
/// <typeparam name="T">The type of expected result.</typeparam>
805+
/// <exception cref="OperationCanceledException">Thrown with no parameters.</exception>
806+
/// <returns>This method always throws, so it actually never returns a value.</returns>
807+
[DoesNotReturn]
808+
public static T ThrowOperationCanceledException<T>()
809+
{
810+
throw new OperationCanceledException();
811+
}
812+
621813
/// <summary>
622814
/// Throws a new <see cref="OperationCanceledException"/>.
623815
/// </summary>
@@ -687,6 +879,18 @@ public static T ThrowOperationCanceledException<T>(string message, Exception inn
687879
throw new OperationCanceledException(message, innerException, token);
688880
}
689881

882+
/// <summary>
883+
/// Throws a new <see cref="PlatformNotSupportedException"/>.
884+
/// </summary>
885+
/// <typeparam name="T">The type of expected result.</typeparam>
886+
/// <exception cref="PlatformNotSupportedException">Thrown with no parameters.</exception>
887+
/// <returns>This method always throws, so it actually never returns a value.</returns>
888+
[DoesNotReturn]
889+
public static T ThrowPlatformNotSupportedException<T>()
890+
{
891+
throw new PlatformNotSupportedException();
892+
}
893+
690894
/// <summary>
691895
/// Throws a new <see cref="PlatformNotSupportedException"/>.
692896
/// </summary>
@@ -714,6 +918,18 @@ public static T ThrowPlatformNotSupportedException<T>(string message, Exception
714918
throw new PlatformNotSupportedException(message, innerException);
715919
}
716920

921+
/// <summary>
922+
/// Throws a new <see cref="SynchronizationLockException"/>.
923+
/// </summary>
924+
/// <typeparam name="T">The type of expected result.</typeparam>
925+
/// <exception cref="SynchronizationLockException">Thrown with no parameters.</exception>
926+
/// <returns>This method always throws, so it actually never returns a value.</returns>
927+
[DoesNotReturn]
928+
public static T ThrowSynchronizationLockException<T>()
929+
{
930+
throw new SynchronizationLockException();
931+
}
932+
717933
/// <summary>
718934
/// Throws a new <see cref="SynchronizationLockException"/>.
719935
/// </summary>
@@ -741,6 +957,18 @@ public static T ThrowSynchronizationLockException<T>(string message, Exception i
741957
throw new SynchronizationLockException(message, innerException);
742958
}
743959

960+
/// <summary>
961+
/// Throws a new <see cref="TimeoutException"/>.
962+
/// </summary>
963+
/// <typeparam name="T">The type of expected result.</typeparam>
964+
/// <exception cref="TimeoutException">Thrown with no parameters.</exception>
965+
/// <returns>This method always throws, so it actually never returns a value.</returns>
966+
[DoesNotReturn]
967+
public static T ThrowTimeoutException<T>()
968+
{
969+
throw new TimeoutException();
970+
}
971+
744972
/// <summary>
745973
/// Throws a new <see cref="TimeoutException"/>.
746974
/// </summary>
@@ -768,6 +996,18 @@ public static T ThrowTimeoutException<T>(string message, Exception innerExceptio
768996
throw new TimeoutException(message, innerException);
769997
}
770998

999+
/// <summary>
1000+
/// Throws a new <see cref="UnauthorizedAccessException"/>.
1001+
/// </summary>
1002+
/// <typeparam name="T">The type of expected result.</typeparam>
1003+
/// <exception cref="UnauthorizedAccessException">Thrown with no parameters.</exception>
1004+
/// <returns>This method always throws, so it actually never returns a value.</returns>
1005+
[DoesNotReturn]
1006+
public static T ThrowUnauthorizedAccessException<T>()
1007+
{
1008+
throw new UnauthorizedAccessException();
1009+
}
1010+
7711011
/// <summary>
7721012
/// Throws a new <see cref="UnauthorizedAccessException"/>.
7731013
/// </summary>
@@ -795,6 +1035,18 @@ public static T ThrowUnauthorizedAccessException<T>(string message, Exception in
7951035
throw new UnauthorizedAccessException(message, innerException);
7961036
}
7971037

1038+
/// <summary>
1039+
/// Throws a new <see cref="Win32Exception"/>.
1040+
/// </summary>
1041+
/// <typeparam name="T">The type of expected result.</typeparam>
1042+
/// <exception cref="Win32Exception">Thrown with no parameters.</exception>
1043+
/// <returns>This method always throws, so it actually never returns a value.</returns>
1044+
[DoesNotReturn]
1045+
public static T ThrowWin32Exception<T>()
1046+
{
1047+
throw new Win32Exception();
1048+
}
1049+
7981050
/// <summary>
7991051
/// Throws a new <see cref="Win32Exception"/>.
8001052
/// </summary>

0 commit comments

Comments
 (0)