The out parameter 'errMsg' must be assigned to before control leavesthe current method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • (2b|!2b)==?

    The out parameter 'errMsg' must be assigned to before control leavesthe current method

    I get this error:

    The out parameter 'errMsg' must be assigned to before control leaves the
    current method

    when compiling the following code:


    public void ExecuteCommands (DDLCommands commands, out string
    errMsg)
    {
    try
    {
    foreach (Command cmd in commands)
    {
    errMsg = "";
    m_cmd.CommandTe xt = cmd.Expression( );
    m_cmd.ExecuteNo nQuery();
    }
    }
    catch (SqlException e)
    {
    errMsg = e.Message;
    Console.WriteLi ne(e.Message);
    }
    }


    I am initing the errMsg variable in the method body - so I dont
    understand the error msg - what am I missing ?
  • Marc Gravell

    #2
    Re: The out parameter 'errMsg' must be assigned to before control leaves the current method

    It doesn't trust you that there is always a command... what if commands is
    an empty list?

    Just set errMsg to blank at the top somewhere. Note also that your code will
    only keep an error if it happens on the last command. Also, I'd prrobably
    recommend throwing an exception rather than using an "out"...

    The simplest way would be to remove the handling entirely and let the first
    exception bubble to the caller; if (for whatever reason - it sounds very
    dangerous to me...) you need to run all the commands and present one
    combined error (if any), then perhaps:

    // not recommended
    StringBuilder sb = null;
    foreach(Command cmd in commands)
    {
    try {
    // blah
    } catch (SqlException ex) {
    if(sb == null) sb = new StringBuilder() ;
    sb.AppendLine(e x.Message);
    }
    }
    if(sb != null) throw new InvalidOperatio nException(sb.T oString());

    Marc


    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: The out parameter 'errMsg' must be assigned to before controlleaves the current method

      (2b|!2b)==? wrote:
      I get this error:
      >
      The out parameter 'errMsg' must be assigned to before control leaves the
      current method
      >
      when compiling the following code:
      >
      >
      public void ExecuteCommands (DDLCommands commands, out string
      errMsg)
      {
      try
      {
      foreach (Command cmd in commands)
      {
      errMsg = "";
      m_cmd.CommandTe xt = cmd.Expression( );
      m_cmd.ExecuteNo nQuery();
      }
      }
      catch (SqlException e)
      {
      errMsg = e.Message;
      Console.WriteLi ne(e.Message);
      }
      }
      >
      >
      I am initing the errMsg variable in the method body - so I dont
      understand the error msg - what am I missing ?
      You are initialising the string in code that is not guaranteed to run.

      The collection could be empty, or the commands.GetEnu merator call could
      possibly throw an exception, which would mean that the code in the loop
      would not run at all. You have to initialise the variable before the loop.

      The compiler might not be able to determine that it's safe to initialise
      the variable as long as you do it inside the try block. You should just
      initialise it the first thing that you do in the method.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: The out parameter 'errMsg' must be assigned to before control leaves the current method

        Göran Andersson wrote:
        (2b|!2b)==? wrote:
        >I get this error:
        >>
        >The out parameter 'errMsg' must be assigned to before control leaves
        >the current method
        >>
        >when compiling the following code:
        >>
        >>
        > public void ExecuteCommands (DDLCommands commands, out string
        >errMsg)
        > {
        > try
        > {
        > foreach (Command cmd in commands)
        > {
        > errMsg = "";
        > m_cmd.CommandTe xt = cmd.Expression( );
        > m_cmd.ExecuteNo nQuery();
        > }
        > }
        > catch (SqlException e)
        > {
        > errMsg = e.Message;
        > Console.WriteLi ne(e.Message);
        > }
        > }
        >>
        >>
        >I am initing the errMsg variable in the method body - so I dont
        >understand the error msg - what am I missing ?
        >
        You are initialising the string in code that is not guaranteed to run.
        >
        The collection could be empty, or the commands.GetEnu merator call
        could possibly throw an exception, which would mean that the code in
        the loop would not run at all. You have to initialise the variable
        before the loop.
        If GetEnumerator throws an exception which causes execution to leave the
        function, there's no requirement to assign output variables.
        (And the case of catching and discarding the exception is properly handled.)

        It's the possibility of an empty collection that's causing the problem.
        >
        The compiler might not be able to determine that it's safe to
        initialise the variable as long as you do it inside the try block.
        You should just initialise it the first thing that you do in the
        method.

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: The out parameter 'errMsg' must be assigned to before control leaves the current method

          Marc Gravell wrote:
          It doesn't trust you that there is always a command... what if
          commands is an empty list?
          >
          Just set errMsg to blank at the top somewhere. Note also that your
          code will only keep an error if it happens on the last command. Also,
          No, it'll stop the loop on the first error.
          I'd prrobably recommend throwing an exception rather than using an
          "out"...
          The simplest way would be to remove the handling entirely and let the
          first exception bubble to the caller; if (for whatever reason - it
          sounds very dangerous to me...) you need to run all the commands and
          present one combined error (if any), then perhaps:
          >
          // not recommended
          StringBuilder sb = null;
          foreach(Command cmd in commands)
          {
          try {
          // blah
          } catch (SqlException ex) {
          if(sb == null) sb = new StringBuilder() ;
          sb.AppendLine(e x.Message);
          }
          }
          if(sb != null) throw new InvalidOperatio nException(sb.T oString());
          >
          Marc

          Comment

          • Marc Gravell

            #6
            Re: The out parameter 'errMsg' must be assigned to before control leaves the current method

            You are right; I read it as being foreach { try {...} catch {...} }, but it
            is actually try { foreach {...} } catch {...}

            My bad...

            Marc


            Comment

            Working...