[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

Unreachable code detected Error

In the code fragment below I am receiving an Unreacable code error.  It is important to trow the exception and also return the -1 value.  I have also included the full method:

ERROR :
if (cnn.Name == newConnection.Name)
{
    throw (new IndexOutOfRangeException("Connection name already exist. "));
    return - 1;  // this is the unreachable code error
}


FULL METHOD:
public int Add(Connection newConnection)
{
      // Invokes Add method of the List object to add a new Connection.
      // We first need to check if another connection with the same name already
      // exist in the list
      foreach (Connection cnn in List)
      {
            if (cnn.Name == newConnection.Name)
            {
                  throw (new IndexOutOfRangeException("Connection name already exist. "));
                  return - 1;
            }
      }
      return List.Add(newConnection);

}  // END: public int Add(Connection newConnection)
0
wenyonk
Asked:
wenyonk
  • 2
  • 2
1 Solution
 
strickddCommented:
Once you throw and exception, the code will stop and anything after the throw will NOT be executed. That is why it is unreachable.
What you can do is a try catch type thing

try
{
    if (cnn.Name == newConnection.Name)
          {
               throw (new IndexOutOfRangeException("Connection name already exist. "));
          }
}
catch ex as IndexOutOfRangeException
{
    return -1;
}


that should work for you to achieve the results you want.
0
 
gena17Commented:
> It is important to trow the exception and also return the -1 value

You can't now throw the exception and also return the -1 value. If you throw the exception nobody can get the -1 any case.
The only way I see is to pass an integer parameter by reference, assign it a value, and after that throw exception (I didn't test it) :

public void Add(Connection newConnection, out int result)
{
     // Invokes Add method of the List object to add a new Connection.
     // We first need to check if another connection with the same name already
     // exist in the list
     foreach (Connection cnn in List)
     {
          if (cnn.Name == newConnection.Name)
          {
               result = -1;
               throw (new IndexOutOfRangeException("Connection name already exist. "));
          }
     }
     result = List.Add(newConnection);
}
0
 
wenyonkAuthor Commented:
strickdd:

I tried the following code now instead of the Unreachable Code Error I get a  The variable 'ex' is assigned but its value is never used error.  even if I assign the varable the error would be that the variable was assigned but never used??

your code was in vb which would allow this to occur and not show as an error however, C# is not so forgiving...


        public int Add(Connection newConnection)
        {
            try
            {
                // Invokes Add method of the List object to add a new Connection.
                // We first need to check if another connection with the same name already exist in the list
                foreach (Connection cnn in List)
                {
                    if (cnn.Name == newConnection.Name)
                    {
                        throw (new IndexOutOfRangeException("Connection name already exist. "));
                    }
                }
                return List.Add(newConnection);
            }
            catch (IndexOutOfRangeException ex)
            {
                return -1;
            }
       }

0
 
wenyonkAuthor Commented:
This is how I resolved the error...

            try
            {
                // Invokes Add method of the List object to add a new Connection.
                // We first need to check if another connection with the same name already
                // exist in the list
                foreach (Connection cnn in List)
                {
                    if (cnn.Name == newConnection.Name)
                    {
                        throw (new IndexOutOfRangeException("Connection name already exist. "));
                    }
                }
                return List.Add(newConnection);
            }
            catch (IndexOutOfRangeException ex)
            {
                if (ex.Message.Length > 0)
                {
                    return -1;
                }
                else
                {
                    return -1;
                }
            }
0
 
strickddCommented:
If you aren't going to use the variable "ex" then don't put it in the statement. You should be able to just do:

catch(IndexOutOfRangeException)
{
      return -1;
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now