Why rethrow an exception?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ahaupt@gmail.com

    Why rethrow an exception?

    Hi all,

    Why would one want to rethrow an exception? Doesn't that defeat the
    purpose?

    Best,
    Andre

  • strawjackal@gmail.com

    #2
    Re: Why rethrow an exception?

    Sometimes you might want to do some tracing logic, or whatever, before
    allowing the exception to be handled by the next tier. Maybe my data
    layer wants to write to a log, then rethrow to the business logic layer
    which catches it and tries the operation again, or simply allows it to
    propagate to the UI layer which displays an error.

    try
    {
    DoSomethingDang erous();
    }
    catch (SillyException ex)
    {
    WriteExceptionT oLog();
    throw; // This will let my UI handle the error as it sees fit.
    }

    Comment

    • Richard

      #3
      RE: Why rethrow an exception?

      -Tracing, sometimes it's good to mark the actual place in code where the
      exception came from. By re-throwing the exception the an additional line of
      diagnostic information is added to the exception's stack trace...

      - To make a more "user friendly" error message. If a raw SQL Server
      exception bubbled up tot he GUI the user would probably not understand it.
      To make a more friendly message I would create a new exception class, with
      ApplicationExce ption as a base, and then pass my "friendly" message and the
      raw exception to the base class constructor... You could also do this if you
      need to internationaliz e your friendly message; you can pass a string
      resource as your message rather than the, probably english, original error
      message...

      - Also, suppose I want to add additional information to my exception such as
      the actual text of a SQL query, or perhaps the user identity, or maybe the
      datetime of the exception. I can do this by making a new exception class,
      again descending from ApplicationExce ption, making the new class
      serializeable, and then adding the additional fields & properties that I need.

      "ahaupt@gmail.c om" wrote:
      [color=blue]
      > Hi all,
      >
      > Why would one want to rethrow an exception? Doesn't that defeat the
      > purpose?
      >
      > Best,
      > Andre
      >
      >[/color]

      Comment

      • gmiley

        #4
        Re: Why rethrow an exception?

        We use catch-throw for several things, namely (as mentioned) obtaining
        more information for troubleshooting on our end (as developers). We
        capture an error, and throw it up further, adding information as
        needed, then allowing our top level ErrorHandler to catch it and write
        out the exception information to a logfile, all while displaying a
        simple message to the user.

        Ifthe EmailOnError bit is set, programming receives a notification of
        the errors occurrance, time, date, briefe description. At that point we
        can check the error.log file for what exactly happened.

        Simply put, it makes it easier for both the user and the developer to
        perform things certain ways.

        Another reason would be if you were writing your own basic libraries.
        If you add an exception type to them and only throw those types the
        higher up programmers will be able to handle your exceptions with
        grace.

        For instance:
        1. I develop library Shared.dll
        2. Shared contains: Shared.BaseValu e, Shared.Calculat eInterest(),
        Shared.Generate PaymentSchedule ()
        3. Shared may also then contain: Shared.Calculat eInterestExcept ion and
        Shared.Generate PaymentSchedule Exception
        4. The programmers who would use my library could put the
        CalculateIntere s method call into a try/catch block and check for that
        specific exception.

        Comment

        • Denny Boynton

          #5
          Re: Why rethrow an exception?

          I agree with everything in the thread and just wanted to add an
          additional note. I see a lot of this in code reviews where exceptions
          are being rethrown:

          catch(Exception ex)
          {
          ...some logging or other activity
          throw ex;
          }

          As the second post on this thread shows, this should always be just
          throw;

          throw ex; obfuscates the original context of the exception.

          I just thought I'd throw that $.02 in.

          Comment

          • billr

            #6
            Re: Why rethrow an exception?

            Another pooint to make (here's my $.02 worth) is that just because you caught
            an Exception does not necessarily mean that your application is in a stable
            state to continue. In this case, you will want the Exception to propogate all
            they way up to your UI where you will ultimately handle it perhaps by doing
            your best to terminate gracefully.


            --
            Of all words of tongue and pen, the saddest are: "It might have been"

            Bill.Richards @ greyskin .co .uk



            "Denny Boynton" wrote:
            [color=blue]
            > I agree with everything in the thread and just wanted to add an
            > additional note. I see a lot of this in code reviews where exceptions
            > are being rethrown:
            >
            > catch(Exception ex)
            > {
            > ...some logging or other activity
            > throw ex;
            > }
            >
            > As the second post on this thread shows, this should always be just
            > throw;
            >
            > throw ex; obfuscates the original context of the exception.
            >
            > I just thought I'd throw that $.02 in.
            >
            >[/color]

            Comment

            • ahaupt@gmail.com

              #7
              Re: Why rethrow an exception?

              Thank you all for your contributions. Sure cleared it up for me.

              Best,
              Andre


              billr wrote:[color=blue]
              > Another pooint to make (here's my $.02 worth) is that just because you caught
              > an Exception does not necessarily mean that your application is in a stable
              > state to continue. In this case, you will want the Exception to propogate all
              > they way up to your UI where you will ultimately handle it perhaps by doing
              > your best to terminate gracefully.
              >
              >
              > --
              > Of all words of tongue and pen, the saddest are: "It might have been"
              >
              > Bill.Richards @ greyskin .co .uk
              > http://greyskin.co.uk
              >
              >
              > "Denny Boynton" wrote:
              >[color=green]
              > > I agree with everything in the thread and just wanted to add an
              > > additional note. I see a lot of this in code reviews where exceptions
              > > are being rethrown:
              > >
              > > catch(Exception ex)
              > > {
              > > ...some logging or other activity
              > > throw ex;
              > > }
              > >
              > > As the second post on this thread shows, this should always be just
              > > throw;
              > >
              > > throw ex; obfuscates the original context of the exception.
              > >
              > > I just thought I'd throw that $.02 in.
              > >
              > >[/color][/color]

              Comment

              • billr

                #8
                Re: Why rethrow an exception?

                Perhaps you would be kind enough to open the responses you found helpful and
                click on the button to indicate that the post was helpful to you.

                Other users in the community will benefit from this action, since they will
                be able to see what other users have deemed helpful responses when they
                search for previous posts pertaining to their current problem (that is
                assuming that the op will search instead of just posting without doing any
                research for themselves)

                Cheers Bill :o))
                --
                Of all words of tongue and pen, the saddest are: "It might have been"

                Bill.Richards @ greyskin .co .uk



                "ahaupt@gmail.c om" wrote:
                [color=blue]
                > Thank you all for your contributions. Sure cleared it up for me.
                >
                > Best,
                > Andre
                >
                >
                > billr wrote:[color=green]
                > > Another pooint to make (here's my $.02 worth) is that just because you caught
                > > an Exception does not necessarily mean that your application is in a stable
                > > state to continue. In this case, you will want the Exception to propogate all
                > > they way up to your UI where you will ultimately handle it perhaps by doing
                > > your best to terminate gracefully.
                > >
                > >
                > > --
                > > Of all words of tongue and pen, the saddest are: "It might have been"
                > >
                > > Bill.Richards @ greyskin .co .uk
                > > http://greyskin.co.uk
                > >
                > >
                > > "Denny Boynton" wrote:
                > >[color=darkred]
                > > > I agree with everything in the thread and just wanted to add an
                > > > additional note. I see a lot of this in code reviews where exceptions
                > > > are being rethrown:
                > > >
                > > > catch(Exception ex)
                > > > {
                > > > ...some logging or other activity
                > > > throw ex;
                > > > }
                > > >
                > > > As the second post on this thread shows, this should always be just
                > > > throw;
                > > >
                > > > throw ex; obfuscates the original context of the exception.
                > > >
                > > > I just thought I'd throw that $.02 in.
                > > >
                > > >[/color][/color]
                >
                >[/color]

                Comment

                • Oliver Sturm

                  #9
                  Re: Why rethrow an exception?

                  billr wrote:
                  [color=blue]
                  > Perhaps you would be kind enough to open the responses you found helpful and
                  > click on the button to indicate that the post was helpful to you.[/color]

                  Could you explain for me what on earth you are talking about? My news
                  reader doesn't have any feedback buttons.


                  Oliver Sturm
                  --
                  omnibus ex nihilo ducendis sufficit unum
                  Spaces inserted to prevent google email destruction:
                  MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
                  ICQ 27142619 http://www.sturmnet.org/blog

                  Comment

                  • billr

                    #10
                    Re: Why rethrow an exception?

                    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


                    there are feedback buttons here

                    --
                    Of all words of tongue and pen, the saddest are: "It might have been"

                    Bill.Richards @ greyskin .co .uk



                    "Oliver Sturm" wrote:
                    [color=blue]
                    > billr wrote:
                    >[color=green]
                    > > Perhaps you would be kind enough to open the responses you found helpful and
                    > > click on the button to indicate that the post was helpful to you.[/color]
                    >
                    > Could you explain for me what on earth you are talking about? My news
                    > reader doesn't have any feedback buttons.
                    >
                    >
                    > Oliver Sturm
                    > --
                    > omnibus ex nihilo ducendis sufficit unum
                    > Spaces inserted to prevent google email destruction:
                    > MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
                    > ICQ 27142619 http://www.sturmnet.org/blog
                    >[/color]

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Why rethrow an exception?

                      billr <billr@discussi ons.microsoft.c om> wrote:[color=blue]
                      > http://msdn.microsoft.com/newsgroups...crosoft.public
                      > .dotnet.languag es.csharp&mid=9 f17c986-6523-4db2-9aa4-235ecc03bd45
                      >
                      > there are feedback buttons here[/color]

                      Please note that not everyone - possibly not even the majority of
                      people - use the web based newsreader. Many of us use dedicated NNTP
                      clients.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...