1. Move your return statement to within the using block.
2. When you throw an exception, you're not returning a value -- not
even within a final IIRC. An exception should unwind the stack.
Exception halts processing, unwinds stack. (Though the exact behavior
of that unwind, I'm not sure, getting that low-level has never been my
strength.) You don't check true/false on a method return for failure
-- you catch exceptions. This ain't the 1970s. If you expect an
exception and absolutely must return an indicator value, .NET
generally prefers a ref parameter for this -- check out the TryParse
pattern implemented for many types in the framework.

∞ Andy Badera
∞ +1 518-641-1280
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera



On Sun, 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