return null upon exception?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Billingsley

    #1

    return null upon exception?

    Suppose I have a method that returns some type of object, and in that method
    I have a try...catch block and just throw my own exception when I catch one.
    The compiler insists that all code paths return a value, so...

    do I just put
    return null;
    after I throw my exception in the catch block?

    or should I initialize the local variable to null and return it in a finally
    block?

    or something else entirely, or is it just personal taste?

    Thanks.


  • Michael Bray

    #2
    Re: return null upon exception?

    "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in
    news:u3UV7BzkDH A.3320@tk2msftn gp13.phx.gbl:
    [color=blue]
    > Suppose I have a method that returns some type of object, and in that
    > method I have a try...catch block and just throw my own exception when
    > I catch one. The compiler insists that all code paths return a value,
    > so...
    >
    > do I just put
    > return null;
    > after I throw my exception in the catch block?
    >
    > or should I initialize the local variable to null and return it in a
    > finally block?
    >
    > or something else entirely, or is it just personal taste?[/color]

    As you say, its pretty much personal taste...

    No matter how you do it, the most important thing is to document the
    function so that the user (perhaps just yourself) knows that null is a
    valid return value (indicating failure), and you should TEST the return
    value in the calling code to see if it is null before using it.

    -mbray

    Comment

    • Marc Lewandowski

      #3
      Re: return null upon exception?

      This doesn't seem to be a correct diagnosis of the error, since (in my
      experience) a "throw" suffices as a "return" for a given code path. For
      instance, this should compile (and run, and throw) with no problem:

      public string ThrowsBeforeRet urn() {
      int x = 1;
      if (x == 1) throw new Exception("Oops !");
      else return "no throw";
      }

      -Marc


      "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in message
      news:u3UV7BzkDH A.3320@tk2msftn gp13.phx.gbl...[color=blue]
      > Suppose I have a method that returns some type of object, and in that[/color]
      method[color=blue]
      > I have a try...catch block and just throw my own exception when I catch[/color]
      one.[color=blue]
      > The compiler insists that all code paths return a value, so...
      >
      > do I just put
      > return null;
      > after I throw my exception in the catch block?
      >
      > or should I initialize the local variable to null and return it in a[/color]
      finally[color=blue]
      > block?
      >
      > or something else entirely, or is it just personal taste?
      >
      > Thanks.
      >
      >[/color]


      Comment

      • mikeb

        #4
        Re: return null upon exception?

        Daniel Billingsley wrote:[color=blue]
        > Suppose I have a method that returns some type of object, and in that method
        > I have a try...catch block and just throw my own exception when I catch one.
        > The compiler insists that all code paths return a value, so...
        >
        > do I just put
        > return null;
        > after I throw my exception in the catch block?
        >
        > or should I initialize the local variable to null and return it in a finally
        > block?
        >
        > or something else entirely, or is it just personal taste?
        >
        > Thanks.
        >
        >[/color]

        A code path that throws an exception does not need to return anything
        (in fact, it can't). You should post a sample of what you're doing and
        the error you get.

        --
        mikeb

        Comment

        • Michael Bray

          #5
          Re: return null upon exception?

          "Marc Lewandowski" <marc@telebill. com> wrote in
          news:OJPpCNzkDH A.1084@tk2msftn gp13.phx.gbl:
          [color=blue]
          > This doesn't seem to be a correct diagnosis of the error, since (in my
          > experience) a "throw" suffices as a "return" for a given code path.
          > For instance, this should compile (and run, and throw) with no
          > problem:
          >
          > public string ThrowsBeforeRet urn() {
          > int x = 1;
          > if (x == 1) throw new Exception("Oops !");
          > else return "no throw";
          > }
          >
          > -Marc[/color]

          You are right - I didn't quite understand the original question (or I
          didn't read it correctly)... It seems, however, after reading the
          original question, that there WAS a code path that wasn't returning a
          value, which would indicate a possible logic error, or just
          inexperienced programming. I'm guessing that they wrote code something
          like:

          public string NoReturnValue()
          {
          int x = 1;
          if (x > 0) return x;
          throw new Exception("Oops ");
          }

          and never "returning a value" at the end of the function.

          As far as code REQUIRING a return value, you are correct - there's
          nothing wrong with the code you've presented. However, I prefer to
          reserve throwing exceptions for unusual circumstances where the error is
          significant enough that the code can't continue. This is different from
          returning an error, which is a "possible expected outcome".

          -mbray

          Comment

          • Allen Anderson

            #6
            Re: return null upon exception?


            the errors don't actually have anything to do with each other. If you
            rethrow your exception in the catch it makes no difference to anything
            you are returning since when the exception comes out its not going to
            return a value anyway.

            you shouldn't have any 'return' or anything after your throw statement
            in your catch block. However, unless the throw is unavoidable in code
            then you need to return something at the end of your routine.


            Allen Anderson

            mailto: allen@put my website url here.com


            On Wed, 15 Oct 2003 11:24:51 -0400, "Daniel Billingsley"
            <dbillingsley@N O.durcon.SPAAMM .com> wrote:
            [color=blue]
            >Suppose I have a method that returns some type of object, and in that method
            >I have a try...catch block and just throw my own exception when I catch one.
            >The compiler insists that all code paths return a value, so...
            >
            >do I just put
            >return null;
            >after I throw my exception in the catch block?
            >
            >or should I initialize the local variable to null and return it in a finally
            >block?
            >
            >or something else entirely, or is it just personal taste?
            >
            >Thanks.
            >[/color]

            Comment

            • Daniel Billingsley

              #7
              Re: return null upon exception?

              Ah, yes you're right.

              (I'm trying to work through some sample code and understand what it's doing
              as I learn C# and Ado.NET)

              It calls a private method in the catch block to actually throw the exception
              so that a trace write can be performed in that method as well - good idea
              for code reuse but not so good for this compiler constraint. I suppose the
              better way to do that would be to put the Trace calls in the custom
              exception's constructor overrides, eh?

              "Marc Lewandowski" <marc@telebill. com> wrote in message
              news:OJPpCNzkDH A.1084@tk2msftn gp13.phx.gbl...[color=blue]
              > This doesn't seem to be a correct diagnosis of the error, since (in my
              > experience) a "throw" suffices as a "return" for a given code path. For
              > instance, this should compile (and run, and throw) with no problem:
              >
              > public string ThrowsBeforeRet urn() {
              > int x = 1;
              > if (x == 1) throw new Exception("Oops !");
              > else return "no throw";
              > }
              >
              > -Marc
              >
              >
              > "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in message
              > news:u3UV7BzkDH A.3320@tk2msftn gp13.phx.gbl...[color=green]
              > > Suppose I have a method that returns some type of object, and in that[/color]
              > method[color=green]
              > > I have a try...catch block and just throw my own exception when I catch[/color]
              > one.[color=green]
              > > The compiler insists that all code paths return a value, so...
              > >
              > > do I just put
              > > return null;
              > > after I throw my exception in the catch block?
              > >
              > > or should I initialize the local variable to null and return it in a[/color]
              > finally[color=green]
              > > block?
              > >
              > > or something else entirely, or is it just personal taste?
              > >
              > > Thanks.
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • mikeb

                #8
                Re: return null upon exception?

                Daniel Billingsley wrote:[color=blue]
                > Ah, yes you're right.
                >
                > (I'm trying to work through some sample code and understand what it's doing
                > as I learn C# and Ado.NET)
                >
                > It calls a private method in the catch block to actually throw the exception
                > so that a trace write can be performed in that method as well - good idea
                > for code reuse but not so good for this compiler constraint. I suppose the
                > better way to do that would be to put the Trace calls in the custom
                > exception's constructor overrides, eh?
                >[/color]

                That would be one way - possibly the best. However, if you do need to
                call another method in your catch block you can cleanly work around this
                problem by having that method return the exception to be thrown instead
                of having it throw the exception itself:

                try {
                // ...
                }
                catch {
                throw( MyPrivateExcept ionLogger());
                }
                [color=blue]
                > "Marc Lewandowski" <marc@telebill. com> wrote in message
                > news:OJPpCNzkDH A.1084@tk2msftn gp13.phx.gbl...
                >[color=green]
                >>This doesn't seem to be a correct diagnosis of the error, since (in my
                >>experience) a "throw" suffices as a "return" for a given code path. For
                >>instance, this should compile (and run, and throw) with no problem:
                >>
                >> public string ThrowsBeforeRet urn() {
                >> int x = 1;
                >> if (x == 1) throw new Exception("Oops !");
                >> else return "no throw";
                >> }
                >>
                >>-Marc
                >>
                >>
                >>"Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in message
                >>news:u3UV7Bzk DHA.3320@tk2msf tngp13.phx.gbl. ..
                >>[color=darkred]
                >>>Suppose I have a method that returns some type of object, and in that[/color]
                >>
                >>method
                >>[color=darkred]
                >>>I have a try...catch block and just throw my own exception when I catch[/color]
                >>
                >>one.
                >>[color=darkred]
                >>>The compiler insists that all code paths return a value, so...
                >>>
                >>>do I just put
                >>>return null;
                >>>after I throw my exception in the catch block?
                >>>
                >>>or should I initialize the local variable to null and return it in a[/color]
                >>
                >>finally
                >>[color=darkred]
                >>>block?
                >>>
                >>>or something else entirely, or is it just personal taste?
                >>>
                >>>Thanks.
                >>>
                >>>[/color]
                >>
                >>[/color]
                >
                >[/color]

                --
                mikeb

                Comment

                • Bruno Jouhier [MVP]

                  #9
                  Re: return null upon exception?

                  Instead of:

                  void ThrowMyExceptio n() { throw new Exception("my message"); }

                  int MyMethod() {
                  if (ok) return 1;
                  else {
                  ThrowMyExceptio n();
                  return 0; // compiler needs a return or throw here because it does
                  not know this will never be reached
                  }
                  }

                  I would suggest:

                  Exception MyException() { return new Exception("my message"); }

                  int MyMethod() {
                  if (ok) return 1;
                  else throw MyException(); // compiler is happy now
                  }

                  I don't know if this is really your problem, but it sounds like it.

                  Bruno.

                  "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> a écrit dans le
                  message de news:O1mGq%23zk DHA.1708@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                  > Ah, yes you're right.
                  >
                  > (I'm trying to work through some sample code and understand what it's[/color]
                  doing[color=blue]
                  > as I learn C# and Ado.NET)
                  >
                  > It calls a private method in the catch block to actually throw the[/color]
                  exception[color=blue]
                  > so that a trace write can be performed in that method as well - good idea
                  > for code reuse but not so good for this compiler constraint. I suppose[/color]
                  the[color=blue]
                  > better way to do that would be to put the Trace calls in the custom
                  > exception's constructor overrides, eh?
                  >
                  > "Marc Lewandowski" <marc@telebill. com> wrote in message
                  > news:OJPpCNzkDH A.1084@tk2msftn gp13.phx.gbl...[color=green]
                  > > This doesn't seem to be a correct diagnosis of the error, since (in my
                  > > experience) a "throw" suffices as a "return" for a given code path. For
                  > > instance, this should compile (and run, and throw) with no problem:
                  > >
                  > > public string ThrowsBeforeRet urn() {
                  > > int x = 1;
                  > > if (x == 1) throw new Exception("Oops !");
                  > > else return "no throw";
                  > > }
                  > >
                  > > -Marc
                  > >
                  > >
                  > > "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in[/color][/color]
                  message[color=blue][color=green]
                  > > news:u3UV7BzkDH A.3320@tk2msftn gp13.phx.gbl...[color=darkred]
                  > > > Suppose I have a method that returns some type of object, and in that[/color]
                  > > method[color=darkred]
                  > > > I have a try...catch block and just throw my own exception when I[/color][/color][/color]
                  catch[color=blue][color=green]
                  > > one.[color=darkred]
                  > > > The compiler insists that all code paths return a value, so...
                  > > >
                  > > > do I just put
                  > > > return null;
                  > > > after I throw my exception in the catch block?
                  > > >
                  > > > or should I initialize the local variable to null and return it in a[/color]
                  > > finally[color=darkred]
                  > > > block?
                  > > >
                  > > > or something else entirely, or is it just personal taste?
                  > > >
                  > > > Thanks.
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...