Skip to content

Commit 93ed834

Browse files
committed
Added missing COMException throw helpers
1 parent 79c8916 commit 93ed834

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Microsoft.Toolkit/Diagnostics/ThrowHelper.Generic.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,47 @@ public static T ThrowArgumentOutOfRangeException<T>(string name, object value, s
201201
}
202202

203203
#if !NETSTANDARD1_4
204+
/// <summary>
205+
/// Throws a new <see cref="COMException"/>.
206+
/// </summary>
207+
/// <typeparam name="T">The type of expected result.</typeparam>
208+
/// <param name="message">The message to include in the exception.</param>
209+
/// <exception cref="COMException">Thrown with the specified parameter.</exception>
210+
/// <returns>This method always throws, so it actually never returns a value.</returns>
211+
[DoesNotReturn]
212+
public static T ThrowCOMException<T>(string message)
213+
{
214+
throw new COMException(message);
215+
}
216+
217+
/// <summary>
218+
/// Throws a new <see cref="COMException"/>.
219+
/// </summary>
220+
/// <typeparam name="T">The type of expected result.</typeparam>
221+
/// <param name="message">The argument name.</param>
222+
/// <param name="innerException">The inner <see cref="Exception"/> to include.</param>
223+
/// <exception cref="COMException">Thrown with the specified parameters.</exception>
224+
/// <returns>This method always throws, so it actually never returns a value.</returns>
225+
[DoesNotReturn]
226+
public static T ThrowCOMException<T>(string message, Exception innerException)
227+
{
228+
throw new COMException(message, innerException);
229+
}
230+
231+
/// <summary>
232+
/// Throws a new <see cref="COMException"/>.
233+
/// </summary>
234+
/// <typeparam name="T">The type of expected result.</typeparam>
235+
/// <param name="message">The argument name.</param>
236+
/// <param name="error">The HRESULT of the errror to include.</param>
237+
/// <exception cref="COMException">Thrown with the specified parameters.</exception>
238+
/// <returns>This method always throws, so it actually never returns a value.</returns>
239+
[DoesNotReturn]
240+
public static T ThrowCOMException<T>(string message, int error)
241+
{
242+
throw new COMException(message, error);
243+
}
244+
204245
/// <summary>
205246
/// Throws a new <see cref="ExternalException"/>.
206247
/// </summary>

Microsoft.Toolkit/Diagnostics/ThrowHelper.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,41 @@ public static void ThrowArgumentOutOfRangeException(string name, object value, s
175175
}
176176

177177
#if !NETSTANDARD1_4
178+
/// <summary>
179+
/// Throws a new <see cref="COMException"/>.
180+
/// </summary>
181+
/// <param name="message">The message to include in the exception.</param>
182+
/// <exception cref="COMException">Thrown with the specified parameter.</exception>
183+
[DoesNotReturn]
184+
public static void ThrowCOMException(string message)
185+
{
186+
throw new COMException(message);
187+
}
188+
189+
/// <summary>
190+
/// Throws a new <see cref="COMException"/>.
191+
/// </summary>
192+
/// <param name="message">The argument name.</param>
193+
/// <param name="innerException">The inner <see cref="Exception"/> to include.</param>
194+
/// <exception cref="COMException">Thrown with the specified parameters.</exception>
195+
[DoesNotReturn]
196+
public static void ThrowCOMException(string message, Exception innerException)
197+
{
198+
throw new COMException(message, innerException);
199+
}
200+
201+
/// <summary>
202+
/// Throws a new <see cref="COMException"/>.
203+
/// </summary>
204+
/// <param name="message">The argument name.</param>
205+
/// <param name="error">The HRESULT of the errror to include.</param>
206+
/// <exception cref="COMException">Thrown with the specified parameters.</exception>
207+
[DoesNotReturn]
208+
public static void ThrowCOMException(string message, int error)
209+
{
210+
throw new COMException(message, error);
211+
}
212+
178213
/// <summary>
179214
/// Throws a new <see cref="ExternalException"/>.
180215
/// </summary>

UnitTests/UnitTests.Shared/Diagnostics/Test_ThrowHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class Test_ThrowHelper
3636
[DataRow(typeof(ArgumentException))]
3737
[DataRow(typeof(ArgumentNullException))]
3838
[DataRow(typeof(ArgumentOutOfRangeException))]
39+
[DataRow(typeof(COMException))]
3940
[DataRow(typeof(ExternalException))]
4041
[DataRow(typeof(FormatException))]
4142
[DataRow(typeof(InsufficientMemoryException))]

0 commit comments

Comments
 (0)