vendredi 7 février 2020

Leaving the code in an else if() blank in order to escape the if statement

I'm sorry for the strange title, I couldn't find the exact wording that I wanted but I'll do my best to explain my question here. Basically I have some code that goes like this

if(both inputs are not null)
{
    Do this
}
else if(both inputs are null)
{

}
else if(one input is null and the other isn't)
{
    throw new Exception("Both inputs must have a value or neither should");
}

if I don't use the middle else if the last else if will throw the exception no matter whether one or both inputs are null. I'm wanting it so that the program ignores this section of code if both inputs are null. I'm using this data to pass to a SQl query and if one of the inputs are null it acts up. I might just be messing up the logic but I was wondering if this is considered bad practice. I can't think of a problem because there isn't a way that this could execute code accidentally. If there is a better way or if this is considered bad practice I would like to hear other ways to go about this. Thanks.

Aucun commentaire:

Enregistrer un commentaire