What exception to throw when two values I expect to be equal are not equal? (C#)

Drew

I feel like this has to be a duplicate, but I've tried looking around and can't find what I'm looking for.

The example I'm working with starts with parsing an XML document. In this XML document the name of a person is stated in two different locations. Later in a different method, I need to use this person's name. I can use any of these two references to the person's name in the XML document since they are the same, or so I expect. I first want to check that they are indeed the same. If they are not equal for whatever reason, I feel like it would be best to throw an exception. Is there an exception for when two values that one would expect are equal are not in fact equal?

I considered ArgumentException, but I'm not sure if that's best since it specifies:

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method.

This isn't the case here since the arguments are fine, it's just that the value of one of the properties (i.e. the person's name) is not what I expect it to be.

I suppose the first question you might have is why do I want to throw an exception. Maybe it's not the best option, but I feel it should be done considering that the reason the two names do not match is because when the XML document was created one of the name-writes did not do its job correctly, which I'd probably want to know. I'm not experienced with error handling, so it's possible that this doesn't mean an exception should be thrown. Any advice would be appreciated.

Etienne Faucher

I first want to check that they are indeed the same. If they are not equal for whatever reason, I feel like it would be best to throw an exception.

You could use InvalidOperationException, since your operation expects the values to be equal. If the values are not equal, your operation is invalid.

You could also define your own exception. You can afterward customize your exception in regards of your situation / objects to compare.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Expect two elements to be equal

From Dev

Expect two elements to be equal

From Dev

In java, when sorting, what's the solution to a specific issue to sort when I have equal values

From Dev

Values are not equal when they should be

From Dev

Displaying button using jquery when two values are equal

From Dev

Why are these two values not equal on arduino?

From Dev

Check if two arrays' values are equal

From Dev

Two equal max values in R

From Dev

Check if two arrays' values are equal

From Dev

Two equal max values in R

From Dev

How do I assert that two HashMap with Javabean values are equal?

From Dev

what is going wrong when I compare to equal elements of String

From Dev

rspec expect date to equal

From Dev

Checking if two arrays are equal - C

From Dev

returns not equal when equal

From Dev

What exception should I throw when method returns invalid result

From Dev

MySQL - Check if two values are equal or both NULL?

From Dev

Why are two equivalent ISODate values not equal?

From Dev

How to check if two boolean values are equal?

From Dev

Select where two column values are not equal

From Dev

How to check if two values in Lua are primitively equal?

From Dev

Laravel Two Values Must Be NOT Equal Validation

From Dev

Two values equal the same add class Red

From Dev

Specify a field with at most two values equal on mysql

From Dev

Find First Value not Equal to Either of Two Values

From Dev

Where-Object - two "equal" values

From Dev

Comparison of two cell values always returns not equal

From Dev

What is the point of partitioning a disk into two equal volumes?

From Dev

What is the use of equal sign two times in a statement

Related Related

HotTag

Archive