Thank you for your input. I tried this before. The compiler does not accept a return statement under a finally block. The compiler message will indicate:
"Control cannot leave the body of a finally clause." Benj On Sep 14, 5:58 pm, Processor Devil <[email protected]> wrote: > Well, I haven't noticed the first line > bool IsQueryRunOK = false; > So yes, it will return false :). > But as Subrato said, it would be good to specify it in finally block. > > 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; > } > finally > { > return IsQueryRunOk; > } > > 2009/9/14 Benj Nunez <[email protected]> > > > > > So does this mean that the function *will* return false (as expected) > > even in the event of an exception? > > That confirms then that the code is correct. Thanks. > > > Benj > > > On Sep 14, 5:19 pm, Raghupathi Kamuni <[email protected]> wrote: > > > bool IsQueryRunOK = false; > > > > IsQueryRunOK is set to false by default :) > > > On Mon, Sep 14, 2009 at 12:41 PM, Processor Devil < > > [email protected] > > > > > wrote: > > > > Well, it depends on situation... > > > > If there is exception, the IsQueryRunOK isn't set and it will return > > null > > > > (not so good), so you should specify IsQueryRunOK as false on the end > > of > > > > exception handle :) > > > > > 2009/9/14 Subrato <[email protected]> > > > > >> 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
