Which of the two exceptions was called? Which of the two exceptions was called? asp.net asp.net

Which of the two exceptions was called?


For this specific scenario you should be using ArgumentNullException and correctly fill it's ParamName property so that you know the argument that is null.

The class ArgumentException also supports the same property but you should used the most specific exception type available.

When using these types of exceptions also be careful when using the constructor that accept both the message and the parameter name. The orders are switched between each exception type:

throw new ArgumentException("message", "paramName");throw new ArgumentNullException("paramName", "message");


Pass the name of the variable (Val1, Val2 etc) in the second argument to the ArgumentException constructor. This becomes the ArgumentException.ParamName property.


Your calling function should not care which line caused the exception. In either case an ArgumentException was thrown, and both should be dealt with the same way.