ArgumentException ParamName convention for multiple parameters and nested parameters
Asked Answered
B

2

6

Consider that I have a class as follows:

class ProductPrice
{
   public string ProductName { get; set; }
   public decimal RegularPrice { get; set; }
   public decimal SalePrice { get; set; }
}

I have a function like this:

public decimal CalculateDiscount(ProductPrice thePriceInfo)
{
   if (thePriceInfo.SalePrice > thePriceInfo.RegularPrice)
      throw new ArgumentException("Sale price cannot be greater than regular price.","?????");

   return (thePriceInfo.RegularPrice-thePriceInfo.SalePrice) / thePriceInfo.RegularPrice;
}

I'm wondering what I should put in for the ParamName parameter (the ????? above) when I call ArgumentException's constructor.

I'm wondering what the standard conventions are for ParamName when:

1) The parameters causing the exception are nested within a class (i.e. thePriceInfo.SalePrice)

2) The exception is due to an interaction between two different arguments (in this case SalePrice being higher than RegularPrice). Do you separate them by commas or something?

Also, is the ParamName parameter actually used for anything in .NET itself, or popular 3rd party tools, or is it just informational, to be used by other calling code further up the stack?

Byran answered 5/2, 2015 at 2:54 Comment(1)
CalculateDiscount does not compile. You need to use thePriceInfo when do the math in the return statement.Mulish
J
4

I can't speak for third-party tools that might use that data, but I don't think there are any conventions. MSDN suggests it is for informational purposes: (for end-users, or for developers who might catch it in another assembly further upstream, so they can handle it accordingly)

The content of paramName is intended to be understood by humans.

So set whatever value you feel conveys the correct message. IMO, the regular price isn't wrong, but the sale price is, so either "SalePrice" or "ProductPrice.SalePrice".


If I could make an observation though...

You don't really have any invalid or out of range arguments here - just a business rule that specifies one amount can't be less than the other. That's not exceptional, and doesn't require an exception.

I'd split your logic in two... one that validates the price, and another that calculates the discount.

public bool IsDiscountValid(ProductPrice pp)
{
    return pp.SalePrice <= pp.RegularPrice;
}

public decimal CalculateDiscount(ProductPrice pp)
{
   return (pp.RegularPrice - pp.SalePrice) / pp.RegularPrice;
}

Now the caller can notify the user in whatever way is most appropriate for your program:

if (IsDiscountValid(thePriceInfo))
    return CalculateDiscount(thePriceInfo);

MessageBox.Show("Sale price cannot be greater than regular price.", "Invalid Price");

// or display text in a Label or whatever, depending on which platform you're using
Jillianjillie answered 5/2, 2015 at 3:11 Comment(0)
M
1

There is nothing wrong with the argument, it is the state of the argument that is in error. I suggest throwingNotSupportedException. From MSDN

For scenarios where it is sometimes possible for the object to perform the requested operation, and the object state determines whether the operation can be performed

Mulish answered 5/2, 2015 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.