0

I have a form in C# for inputting some data into a List. The form consists of text boxes and up and down numeric boxes. Everything works fine but I want to have an error handler (try/catch) in my code so it will check if any of the text boxes are empty or the numeric boxes are left to 0, if thats the case it should pop up an error message. I tried :

try 
{
  //code
}
catch (NoNullAllowedException e) //tried it without the e as well
{
  //code
}

The code I was having in the brackets its the following one. Sometimes the GetItemDetails() was throwing me an error saying that not all code paths returns a value.

Any thoughts why is doing this or how can I fix it?

   public iRepairable GetItemDetails()
    {

            Shirt shirt = null;
            TShirt tshirt = null;
            Trouser trouser = null;
            Shoe shoe = null;

            Boolean isShirt = true;
            Boolean isTshirt = true;
            Boolean isTrouser = true;
            Boolean isShoe = true;

        if (rdoShirt.Checked == true)
                {
                    shirt = new Shirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);

                    isTshirt = false;
                    isTrouser = false;
                    isShoe = false;
                }
                else if (rdoTShirt.Checked == true)
                {
                    tshirt = new TShirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);

                    isShirt = false;
                    isTrouser = false;
                    isShoe = false;
                }
                else if (rdoTrouser.Checked == true)
                {
                    trouser = new Trouser(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);

                    isShirt = false;
                    isTshirt = false;
                    isShoe = false;
                }
                else
                {
                    shoe = new Shoe(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);

                    isShirt = false;
                    isTrouser = false;
                    isTshirt = false;
                }



                if (isShirt)
                {
                    return shirt;
                }
                else if (isTshirt)
                {
                    return tshirt;
                }
                else if (isTrouser)
                {
                    return trouser;
                }
                else //if(isShoe)
                {
                    return shoe;
                }
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • Look at this question: http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value – jvanrhyn Jan 11 '16 at 20:28
  • I saw that mate, but i didnt find it much helpfull... i wrote exceptions b4 but nothing like that came up... –  Jan 11 '16 at 20:34
  • The code path not returning anything is the one where not one of your if statements are true, so adding a `return null;` outside all your if statements should sort that. Also, your catch must also either return a result or re-throw the exception. – jvanrhyn Jan 11 '16 at 20:41
  • what do you mean exactly? is there any example i can see? Everything works fine until I put the try/catch. When I put that, the GetItemsDetails() throws that error... @jvanrhyn –  Jan 11 '16 at 20:44
  • `catch (NoNullAllowedException) { throw; }` or `catch (NoNullAllowedException) { return null; }`. Your catch block is also a code path that needs to return something. – jvanrhyn Jan 11 '16 at 20:47

1 Answers1

0

First of all, the NoNullAllowedException is not for list or just null values. Is the exception that throws when you want to insert null values in a column that doesn't allow them (for more info, MSDN).

For your code, place at the bottom of your code a default return value (but as far as i can see, your code shouldnt break at all)

Okengroth
  • 83
  • 11