Throw exceptions or not when checking method parameters?

I like to verify that all method parameters have the correct information before doing anything with them. Something like that:

public method(MyType param1) { try { if(param1 == null) { throw new ArgumentNullException("Error1"); } if(param1.Property1 == null) { throw new ArgumentNullException("Error2"); } if(param1.Property1.Amount <= 0) { throw new ArgumentException("Error3"); } ... //Do what I need with the parameter } catch { throw; } } 

However, in this post, someone comments that it is not recommended to throw an exception as a regular thread, but I'm not sure if it is so or not, because if I need to check the parameters, there are also exceptions like ArgumentNullException and ArgumentException , which it seems that it can be thrown when there is some kind of problem with the parameters, it makes me wonder if this is really a bad way to do, an example that I comment on.

Another reason another user gives is because the exception consumes 4000-8000 CPU cycles. So, the goal in my case is to know if there is some kind of error with the parameters, and indeed, if the application works as expected, the exception will never be thrown, so in practice, if the application has no errors, t decreases.

So, in general, I would like to know how to best handle parameter checking before continuing with the process.

Thanks.

+5
source share
2 answers

Throwing exceptions after validation is a good approach. Thus, you explicitly let the developer using your method know that he is calling it incorrectly, which allows him to easily fix this error.

I would definitely get rid of the try...catch . This is completely redundant and makes the code more complex than it should be (and a bit confusing).

+1
source

You should definitely throw exceptions when the required value is zero or missing. One of the things I like to clear this type of situation is to use the type validation method and use Exception annotations when my code can throw an exception.

The downside is that if you use the Validate (...) method correctly, it is called twice. I like the Validate (...) approach because it allows you to change the error detection before throwing an exception, because any class can call Validate (...)

 class MyClass { /// <summary> /// DoSomething /// </summary> /// <exception cref="InvalidOperationException">InvalidOperationException</exception> public void DoSomething(MyType myType) { string errorMessage; if (!Validate(myType, out errorMessage)) throw new InvalidOperationException(string.Format("Argument myType is not valid: {0}", errorMessage)); // Finish } /// <summary> /// IsValid /// </summary> /// <exception cref="ArgumentNullException">ArgumentNullException</exception> public static bool Validate(MyType myType, out string errorMessage) { errorMessage = null; if (myType == null) throw new ArgumentNullException("myType"); if (string.IsNullOrEmpty(myType.Property1)) errorMessage = "Property1 is required"; if (string.IsNullOrEmpty(myType.Property2)) errorMessage = "Property2 is required"; return errorMessage == null; } } class MyType { public string Property1 { get; set; } public string Property2 { get; set; } } 
+2
source

Source: https://habr.com/ru/post/1270280/


All Articles