Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic Throw class: good or bad practice
    primarykey
    data
    text
    <p>Throwing exceptions often follows the following pattern:</p> <pre><code>if(condition) { throw exception; } </code></pre> <p>you check a condition, and if the condition is satisfied, you throw an exception. So, i was wondering if it is good idea to write a static class for it that could look like this:</p> <pre><code>public static class Throw { public static void IfNullOrEmpty&lt;T&gt;(string @string, params object[] parameters) where T : Exception { Throw.If&lt;T&gt;(string.IsNullOrEmpty(@string), parameters); } public static void IfNullOrEmpty&lt;T, I&gt;(IEnumerable&lt;I&gt; enumerable, params object[] parameters) where T : Exception { Throw.If&lt;T&gt;(enumerable == null || enumerable.Count() == 0, parameters); } public static void IfNullOrEmpty(string @string, string argumentName) { Throw.IfNullOrEmpty(@string, argumentName, string.Format("Argument '{0}' cannot be null or empty.", argumentName)); } public static void IfNullOrEmpty(string @string, string argumentName, string message) { Throw.IfNullOrEmpty&lt;ArgumentNullOrEmptyException&gt;(@string, message, argumentName); } public static void IfNullOrEmpty&lt;I&gt;(IEnumerable&lt;I&gt; enumerable, string argumentName) { Throw.IfNullOrEmpty(enumerable, argumentName, string.Format("Argument '{0}' cannot be null or empty.", argumentName)); } public static void IfNullOrEmpty&lt;I&gt;(IEnumerable&lt;I&gt; enumerable, string argumentName, string message) { Throw.IfNullOrEmpty&lt;ArgumentNullOrEmptyException, I&gt;(enumerable, message, argumentName); } public static void IfNull&lt;T&gt;(object @object, params object[] parameters) where T : Exception { Throw.If&lt;T&gt;(@object == null, parameters); } public static void If&lt;T&gt;(bool condition, params object[] parameters) where T : Exception { if (condition) { var types = new List&lt;Type&gt;(); var args = new List&lt;object&gt;(); foreach (object p in parameters ?? Enumerable.Empty&lt;object&gt;()) { types.Add(p.GetType()); args.Add(p); } var constructor = typeof(T).GetConstructor(types.ToArray()); var exception = constructor.Invoke(args.ToArray()) as T; throw exception; } } public static void IfNull(object @object, string argumentName) { Throw.IfNull&lt;ArgumentNullException&gt;(@object, argumentName); } } </code></pre> <p>(Note: The <code>ArgumentNullOrEmptyException</code>is not defined here, but it does pretty much what one would expect.)</p> <p>so instead of repeatedly writing stuff like that</p> <pre><code>void SomeFunction(string someParameter) { if(string.IsNullOrEmpty(someParameter)) { throw new ArgumentNullOrEmptyException("someParameter", "Argument 'someParameter' cannot be null or empty."); } } </code></pre> <p>i just do</p> <pre><code>void SomeFunction(string someParameter) { Throw.IfNullOrEmpty(someParameter, "someParameter"); // not .IsNullOrEmpty } </code></pre> <p>i actually do like it, but is it also a good practice?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload