You should put that return statement in finally block that would help you to return no matter what the situation. (I am answering this without actually running or debugging the code so excuse me if that doesn't help)

On Sep 13, 2009, at 10:25 PM, Benj Nunez <[email protected]> wrote:


Hello everyone,

This has been nagging me for quite some time now, perhaps you can
enlighten me on this.
You see I have a function that returns either true or false. But what
will happen if underneath
this function, there's a call that would raise an exception?



      public bool runQueryEx(String AQueryString)
       {
           bool IsQueryRunOK = false;

           using (OleDbCommand cmd = new OleDbCommand(AQueryString,
oleconn))
           {
               cmd.CommandType = CommandType.Text;

               try
               {
                   cmd.ExecuteNonQuery();
                   IsQueryRunOK = true;
               }
               catch (System.InvalidOperationException)
               {
                   //MessageBox.Show(ioe.Message, "Error",
                   //    MessageBoxButtons.OK, MessageBoxIcon.Error);
                   throw;
               }
               catch (System.Exception)
               {
                   //MessageBox.Show(e.Message, "Error",
                   //    MessageBoxButtons.OK, MessageBoxIcon.Error);
                   throw;
               }
           }

           return IsQueryRunOK;     // will it return false in case
of exception ?
       }


When debugging, I noticed that the last line:

           return IsQueryRunOK;


seems to get skipped *whenever* there's an exception. Is this normal?
Is it implied
that since it's a function it should return a value? Or just to be
safe (and for peace of mind),  should I use a finally block? What
would you recommend?




Benj



Reply via email to