0

In connection with this question: How to convert UPC-E to UPC-A?, I found and adapted a method to convert UPC-E barcodes to UPC-A:

public static string ConvertUPCEToUPCA(string UPCE)
{
    try
    {
        int UPCLen;
        string UPCA = "";
        int chkInt;

        UPCLen = UPCE.Length; 
        FormatString formatString = new FormatString();
        if (formatString.containsAlpha(UPCE) != -1) 
        {
            UPCLen = 11;
        }

        if (UPCLen == 7) // Didn't enter a check digit
        {
            UPCE = UPCE.Substring(1,6);
        }
        else if (UPCLen == 8) // Entered both check digit and leading 0 digit
        {
            UPCE = UPCE.Substring(2,6);
        }

        if (UPCLen < 9)
        {
            chkInt = Convert.ToInt32(UPCE.Substring(5,1));

            switch (chkInt)
            {
                case 0: 
                case 1: 
                case 2:
                    UPCA = UPCE.Substring(0,2) + UPCE.Substring(5,1) + "0000" + UPCE.Substring(2,3);
                    break;
                case 3:
                    UPCA = UPCE.Substring(0,3) + "00000" + UPCE.Substring(3,2);
                    break;
                case 4:
                    UPCA = UPCE.Substring(0,4) + "00000" + UPCE.Substring(4,1);
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    UPCA = UPCE.Substring(0,5) + "0000" + UPCE.Substring(5,1);
                    break;
                default:
                    break;
            }
            UPCA = "0" + UPCA;
        }
        return UPCA;
    }
    catch(Exception ex)
    {
        Duckbill.ExceptionHandler(ex, "PlatypusUtils.ConvertUPCEToUPCA(UPCE)");
    }
}

...but I'm getting, "Not all code paths return a value." Why? Isn't the "return UPCA;" line reached no matter what?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

4

You're not returning anything should an Exception happen. Think of it: you've caught an Exception and you've dealt with it. Now what? The caller is expecting some return from your method, and your method isn't giving off any.

You should either:

  • Return a string in the catch block;
  • Throw an Exception in the catch block - either the one you caught, or a new one;
  • Create a variable that stores the value you want to return. Assign it either a null, empty string, or any other "default" value before the try block. As the last thing in your try block, assign the resulting value to this variable. Return it in a finally block. So if an error happens, you at least have something to return. Otherwise you return the result of your operations.

Choose the option that suits you best based on your requirements. You can also try some variation of those.

Edit: I've just re-read the question and paid special attention to the last sentence.

Isn't the "return UPCA;" line reached no matter what?

No. Should an error happen somewhere, the try block stops at the offending line. Execution goes straight to the first catch block that can handle the thrown Exception (there's only one in your case anyway).

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
2

You should ALWAYS return a value, even if the code throws an exception, because you're using a method which returns a string and isn't a void method(which doesn't return anything)

You should return a string between the Catch brackets.

Max
  • 12,622
  • 16
  • 73
  • 101