From 04ae971a42cad8aa7a3af05828bd7c24b266c21f Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Tue, 2 Jan 2024 23:14:00 +0800 Subject: [PATCH] feature: add throw util --- .../Exceptions/ThrowExceptionUtility.cs | 48 +++++++++++++++++++ src/c#/GeneralUpdate.Upgrad/Program.cs | 10 ++-- 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs diff --git a/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs new file mode 100644 index 00000000..013a2534 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs @@ -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(args.ToString(), args); + + #region Common + + /// + /// Checks if an object is empty and throws an exception if it is + /// + /// + /// + /// + public static void ThrowIfNull(object obj, string paramName) + { + if (obj == null) + Throw(paramName); + } + + /// + /// Checks if the string is empty or blank, and throws an exception if it is. + /// + /// + /// + /// + public static void ThrowIfNullOrWhiteSpace(string str, string paramName) + { + if (string.IsNullOrWhiteSpace(str)) + Throw("Parameter cannot be null or whitespace", paramName); + } + + /// + /// Basic method of exception declaration. + /// + /// + /// + /// + public static void Throw(string message, params object[] args) where T : Exception, new() + => throw (T)Activator.CreateInstance(typeof(T), message, args); + + #endregion + } +} diff --git a/src/c#/GeneralUpdate.Upgrad/Program.cs b/src/c#/GeneralUpdate.Upgrad/Program.cs index 6189220b..0a9e88af 100644 --- a/src/c#/GeneralUpdate.Upgrad/Program.cs +++ b/src/c#/GeneralUpdate.Upgrad/Program.cs @@ -50,11 +50,11 @@ private static void Main(string[] args) .AddListenerMultiDownloadError(OnMultiDownloadError) //整个更新过程出现的任何问题都会通过这个事件通知 .AddListenerException(OnException) - .Strategy(). - Option(UpdateOption.Encoding, Encoding.Default). - Option(UpdateOption.DownloadTimeOut, 60). - Option(UpdateOption.Format, Format.ZIP). - LaunchTaskAsync(); + .Strategy() + .Option(UpdateOption.Encoding, Encoding.Default) + .Option(UpdateOption.DownloadTimeOut, 60) + .Option(UpdateOption.Format, Format.ZIP) + .LaunchTaskAsync(); }); Console.Read(); }