-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using GeneralUpdate.Core.Exceptions.CustomArgs; | ||
using System; | ||
|
||
namespace GeneralUpdate.Core.Exceptions | ||
{ | ||
internal sealed class ThrowExceptionUtility | ||
{ | ||
public static void ThrowGeneralUpdateException(ExceptionArgs args) | ||
=> Throw<Exception>(args.ToString(), args); | ||
|
||
#region Common | ||
|
||
/// <summary> | ||
/// Checks if an object is empty and throws an exception if it is | ||
/// </summary> | ||
/// <param name="obj"></param> | ||
/// <param name="paramName"></param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public static void ThrowIfNull(object obj, string paramName) | ||
{ | ||
if (obj == null) | ||
Throw<ArgumentNullException>(paramName); | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the string is empty or blank, and throws an exception if it is. | ||
/// </summary> | ||
/// <param name="str"></param> | ||
/// <param name="paramName"></param> | ||
/// <exception cref="ArgumentException"></exception> | ||
public static void ThrowIfNullOrWhiteSpace(string str, string paramName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(str)) | ||
Throw<ArgumentException>("Parameter cannot be null or whitespace", paramName); | ||
} | ||
|
||
/// <summary> | ||
/// Basic method of exception declaration. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="message"></param> | ||
/// <param name="args"></param> | ||
public static void Throw<T>(string message, params object[] args) where T : Exception, new() | ||
=> throw (T)Activator.CreateInstance(typeof(T), message, args); | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters