Throwing Exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Wood

    Throwing Exceptions

    In code that I have enclosed in a try block, it's fairly common for me to
    respond to error conditions with something like this:

    throw new Exception("Some thing went horribly wrong!");

    But I've noticed that system execptions contain information such as the line
    number where the error occurred, while exceptions created using the line
    above do not.

    What is the secret for exceptions thrown by me to have detailed information
    such as the line number?

    Thanks.

    Jonathan

  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Throwing Exceptions

    On Jun 4, 1:14 pm, "Jonathan Wood" <jw...@softcirc uits.comwrote:
    In code that I have enclosed in a try block, it's fairly common for me to
    respond to error conditions with something like this:
    >
    throw new Exception("Some thing went horribly wrong!");
    >
    But I've noticed that system execptions contain information such as the line
    number where the error occurred, while exceptions created using the line
    above do not.
    >
    What is the secret for exceptions thrown by me to have detailed information
    such as the line number?
    >
    Thanks.
    >
    Jonathan
    Hi,

    It should, only that it will be where you throw the exception.
    You can do several things.
    - rethrow the same exception by using throw ;
    - adding the thrown exception as the InnerException to the new
    exception

    Comment

    • Peter Duniho

      #3
      Re: Throwing Exceptions

      On Wed, 04 Jun 2008 10:14:11 -0700, Jonathan Wood <jwood@softcirc uits.com>
      wrote:
      [...]
      But I've noticed that system execptions contain information such as the
      line number where the error occurred, while exceptions created using the
      line above do not.
      >
      What is the secret for exceptions thrown by me to have detailed
      information such as the line number?
      The Exception instance has a StackTrace member that includes this
      information.

      I suppose you could also get the curent StackTrace yourself and format
      that into your error message, but I'm not sure I see the point, given that
      the Exception already has this information.

      Pete

      Comment

      • Jonathan Wood

        #4
        Re: Throwing Exceptions

        Sorry if I wasn't clear. There is no InnerException or exception to rethrow.
        I'm creating my own exception. But it doesn't have line number information.

        --
        Jonathan Wood
        SoftCircuits Programming


        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
        message
        news:58379f4d-0c4b-47bb-9685-c58c72fb0402@z1 6g2000prn.googl egroups.com...
        On Jun 4, 1:14 pm, "Jonathan Wood" <jw...@softcirc uits.comwrote:
        In code that I have enclosed in a try block, it's fairly common for me to
        respond to error conditions with something like this:
        >
        throw new Exception("Some thing went horribly wrong!");
        >
        But I've noticed that system execptions contain information such as the
        line
        number where the error occurred, while exceptions created using the line
        above do not.
        >
        What is the secret for exceptions thrown by me to have detailed
        information
        such as the line number?
        >
        Thanks.
        >
        Jonathan
        Hi,

        It should, only that it will be where you throw the exception.
        You can do several things.
        - rethrow the same exception by using throw ;
        - adding the thrown exception as the InnerException to the new
        exception

        Comment

        • Jonathan Wood

          #5
          Re: Throwing Exceptions

          It doesn't include line number information for exceptions that I create.

          It does have stack trace information so there's no reason to mess with that.

          --
          Jonathan Wood
          SoftCircuits Programming


          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.ub8koi2 c8jd0ej@petes-computer.local. ..
          On Wed, 04 Jun 2008 10:14:11 -0700, Jonathan Wood <jwood@softcirc uits.com>
          wrote:
          >
          >[...]
          >But I've noticed that system execptions contain information such as the
          >line number where the error occurred, while exceptions created using the
          >line above do not.
          >>
          >What is the secret for exceptions thrown by me to have detailed
          >information such as the line number?
          >
          The Exception instance has a StackTrace member that includes this
          information.
          >
          I suppose you could also get the curent StackTrace yourself and format
          that into your error message, but I'm not sure I see the point, given that
          the Exception already has this information.
          >
          Pete

          Comment

          • Peter Duniho

            #6
            Re: Throwing Exceptions

            On Wed, 04 Jun 2008 12:06:48 -0700, Jonathan Wood <jwood@softcirc uits.com>
            wrote:
            It doesn't include line number information for exceptions that I create.
            Really? When you call StackFrame.GetF ileLineNumber() on the frame of the
            StackTrace you're interested (the lowest frame, for example, which would
            be where the exception was thrown), you get something other than the
            correct line number?

            That's odd.
            It does have stack trace information so there's no reason to mess with
            that.
            The stack trace information should include line numbers for every frame in
            the stack for which debug symbols exist (which presumably would include
            all of your own code).

            Pete

            Comment

            • Jonathan Wood

              #7
              Re: Throwing Exceptions

              Peter,
              Really? When you call StackFrame.GetF ileLineNumber() on the frame of the
              StackTrace you're interested (the lowest frame, for example, which would
              be where the exception was thrown), you get something other than the
              correct line number?
              I haven't tried that. Don't even know what it means. Exception doesn't have
              a StackFrame member, nor does Exception.Stack Trace.

              What I'm doing is displaying the exception using plain-ol' ex.ToString(). In
              exceptions generated by the frameworks, this produces a description of the
              error, followed by stack trace information, followed by the line that
              generated the error. In exceptions that I create, it produces a description
              of the error, followed by stack trace information, but no line number. I'd
              like my own exceptions to produce line numbers in this scenario.
              The stack trace information should include line numbers for every frame in
              the stack for which debug symbols exist (which presumably would include
              all of your own code).
              Here's what ex.ToString() produces for an exception that I created and
              threw:

              System.Exceptio n: Invalid menu item information received (2975)
              at ASP.fooddelete_ aspx.Page_Load( Object sender, EventArgs e)

              --
              Jonathan Wood
              SoftCircuits Programming


              Comment

              • Peter Duniho

                #8
                Re: Throwing Exceptions

                On Wed, 04 Jun 2008 15:05:25 -0700, Jonathan Wood <jwood@softcirc uits.com>
                wrote:
                >Really? When you call StackFrame.GetF ileLineNumber() on the frame of
                >the StackTrace you're interested (the lowest frame, for example, which
                >would be where the exception was thrown), you get something other than
                >the correct line number?
                >
                I haven't tried that. Don't even know what it means. Exception doesn't
                have a StackFrame member, nor does Exception.Stack Trace.
                A StackTrace describes the entire call stack at the time of the
                exception. A StackFrame is a single frame withing the call stack, and you
                can retrieve the StackFrame instances from the StackTrace using the
                GetFrame() or GetFrames() method.

                Given that this information is already embedded in the Exception, via the
                StackTrace, it's my opinion that adding it to the message itself is of
                little value. However, if you really insist, you can always get the
                current StackTrace before you create the Exception, and incorporate the
                line number information into the string you use for the Exception.

                Pete

                Comment

                • Jonathan Wood

                  #9
                  Re: Throwing Exceptions

                  Again, I do not want to add the line number to the Message property. I want
                  the line number to appear in the string returned by the ToString() method,
                  in just the same way it does when called on exceptions that were generated
                  by the framework.

                  I will see if I can see what you are referring to with regards to the
                  GetFrame() method, but it doesn't address the primary thing I'm trying to
                  understand/accomplish.

                  Jonathan

                  "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                  news:op.ub802ho 18jd0ej@petes-computer.local. ..
                  On Wed, 04 Jun 2008 15:05:25 -0700, Jonathan Wood <jwood@softcirc uits.com>
                  wrote:
                  >
                  >>Really? When you call StackFrame.GetF ileLineNumber() on the frame of
                  >>the StackTrace you're interested (the lowest frame, for example, which
                  >>would be where the exception was thrown), you get something other than
                  >>the correct line number?
                  >>
                  >I haven't tried that. Don't even know what it means. Exception doesn't
                  >have a StackFrame member, nor does Exception.Stack Trace.
                  >
                  A StackTrace describes the entire call stack at the time of the
                  exception. A StackFrame is a single frame withing the call stack, and you
                  can retrieve the StackFrame instances from the StackTrace using the
                  GetFrame() or GetFrames() method.
                  >
                  Given that this information is already embedded in the Exception, via the
                  StackTrace, it's my opinion that adding it to the message itself is of
                  little value. However, if you really insist, you can always get the
                  current StackTrace before you create the Exception, and incorporate the
                  line number information into the string you use for the Exception.
                  >
                  Pete

                  Comment

                  • Peter Duniho

                    #10
                    Re: Throwing Exceptions

                    On Wed, 04 Jun 2008 17:50:19 -0700, Jonathan Wood <jwood@softcirc uits.com>
                    wrote:
                    Again, I do not want to add the line number to the Message property. I
                    want the line number to appear in the string returned by the ToString()
                    method, in just the same way it does when called on exceptions that were
                    generated by the framework.
                    Ah. Then you need to sub-class Exception and implement ToString()
                    yourself, so that it returns what you want.
                    I will see if I can see what you are referring to with regards to the
                    GetFrame() method, but it doesn't address the primary thing I'm trying
                    to understand/accomplish.
                    Well, you will need to understand it if you're going to include the line
                    number in your ToString() method. In that sense, it does address the
                    primary thing you're doing.

                    Pete

                    Comment

                    • Jonathan Wood

                      #11
                      Re: Throwing Exceptions

                      Okay, so you're saying the even though Exception.ToStr ing() does include
                      stack trace information, it does not include the line number. But that there
                      are system exceptions that override ToString() such that it does include the
                      line number.

                      If that's the case, I can deal with that. In fact, this is on a Web site
                      where I'm currently having exception data emailed to me. So I could simply
                      have my common handler form the proper string if regular Exceptions include
                      the line number.

                      I guess I figured that Exception.ToStr ing() was including the line number if
                      it was available. So, if not, then you've helped me understand this better.

                      Thanks.

                      --
                      Jonathan Wood
                      SoftCircuits Programming


                      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                      news:op.ub8419s t8jd0ej@petes-computer.local. ..
                      On Wed, 04 Jun 2008 17:50:19 -0700, Jonathan Wood <jwood@softcirc uits.com>
                      wrote:
                      >
                      >Again, I do not want to add the line number to the Message property. I
                      >want the line number to appear in the string returned by the ToString()
                      >method, in just the same way it does when called on exceptions that were
                      >generated by the framework.
                      >
                      Ah. Then you need to sub-class Exception and implement ToString()
                      yourself, so that it returns what you want.
                      >
                      >I will see if I can see what you are referring to with regards to the
                      >GetFrame() method, but it doesn't address the primary thing I'm trying
                      >to understand/accomplish.
                      >
                      Well, you will need to understand it if you're going to include the line
                      number in your ToString() method. In that sense, it does address the
                      primary thing you're doing.
                      >
                      Pete

                      Comment

                      • Jonathan Wood

                        #12
                        Re: Throwing Exceptions

                        Peter,
                        A StackTrace describes the entire call stack at the time of the
                        exception. A StackFrame is a single frame withing the call stack, and you
                        can retrieve the StackFrame instances from the StackTrace using the
                        GetFrame() or GetFrames() method.
                        An example of this would be really, REALLY helpful for me.

                        According to Intellisense, Exception.Stack Trace is a string. Therefore, it
                        has no GetFrame() or GetFrames() method. I've looked high and low for these
                        methods. Any chance you can be a little more specific about where they are?

                        Jonathan

                        Comment

                        • Peter Duniho

                          #13
                          Re: Throwing Exceptions

                          On Wed, 04 Jun 2008 18:40:51 -0700, Jonathan Wood <jwood@softcirc uits.com>
                          wrote:
                          Peter,
                          >
                          >A StackTrace describes the entire call stack at the time of the
                          >exception. A StackFrame is a single frame withing the call stack, and
                          >you can retrieve the StackFrame instances from the StackTrace using the
                          >GetFrame() or GetFrames() method.
                          >
                          An example of this would be really, REALLY helpful for me.
                          >
                          According to Intellisense, Exception.Stack Trace is a string. Therefore,
                          it has no GetFrame() or GetFrames() method. I've looked high and low for
                          these methods. Any chance you can be a little more specific about where
                          they are?
                          The methods are in the StackTrace class.

                          That said, my original reply was incorrect. I misremembered what the
                          StackTrace property provided, and mistakenly implied that it was an actual
                          StackTrace instance. Sorry about that.

                          But, since you are apparently going to need a sub-class anyway, it doesn't
                          matter. In your constructor, you can just use Environment.Sta ckTrace to
                          get the actual StackTrace instance, where you can either save that for
                          later use, or just immediately convert it to whatever string information
                          you actually want (including line numbers).

                          Pete

                          Comment

                          • Jonathan Wood

                            #14
                            Re: Throwing Exceptions

                            Environment.Sta ckTrace is also a string.

                            At any rate, none of this is coming together. I did some additional
                            searching and it looks like it might be an issue of whether or not debugging
                            is enabled and/or the PDB file is available. Although I don't exactly
                            recall, I may have been seeing those line numbers during testing on my
                            desktop machine, but now fail to get them on the Web server.

                            As near as I can tell, ex.ToString() will include stack trace information
                            and also line number information if it is available. It appears the
                            configuration on my Web server does not provide line number information.

                            Jonathan

                            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                            news:op.ub86kt1 d8jd0ej@petes-computer.local. ..
                            On Wed, 04 Jun 2008 18:40:51 -0700, Jonathan Wood <jwood@softcirc uits.com>
                            wrote:
                            >
                            >Peter,
                            >>
                            >>A StackTrace describes the entire call stack at the time of the
                            >>exception. A StackFrame is a single frame withing the call stack, and
                            >>you can retrieve the StackFrame instances from the StackTrace using the
                            >>GetFrame() or GetFrames() method.
                            >>
                            >An example of this would be really, REALLY helpful for me.
                            >>
                            >According to Intellisense, Exception.Stack Trace is a string. Therefore,
                            >it has no GetFrame() or GetFrames() method. I've looked high and low for
                            >these methods. Any chance you can be a little more specific about where
                            >they are?
                            >
                            The methods are in the StackTrace class.
                            >
                            That said, my original reply was incorrect. I misremembered what the
                            StackTrace property provided, and mistakenly implied that it was an actual
                            StackTrace instance. Sorry about that.
                            >
                            But, since you are apparently going to need a sub-class anyway, it doesn't
                            matter. In your constructor, you can just use Environment.Sta ckTrace to
                            get the actual StackTrace instance, where you can either save that for
                            later use, or just immediately convert it to whatever string information
                            you actually want (including line numbers).
                            >
                            Pete

                            Comment

                            • Peter Duniho

                              #15
                              Re: Throwing Exceptions

                              On Wed, 04 Jun 2008 19:24:30 -0700, Jonathan Wood <jwood@softcirc uits.com>
                              wrote:
                              Environment.Sta ckTrace is also a string.
                              Note to self: stop trying to write posts while fixing dinner. :)

                              Anyway, I had that property in mind because I was reading something else
                              at the time. All you need to do is construct a new StackTrace instance.
                              It will automatically retrieve the current stack trace information.
                              At any rate, none of this is coming together. I did some additional
                              searching and it looks like it might be an issue of whether or not
                              debugging is enabled and/or the PDB file is available. Although I don't
                              exactly recall, I may have been seeing those line numbers during testing
                              on my desktop machine, but now fail to get them on the Web server.
                              Well, that's correct...unles s you have the correct symbols with your
                              executable, the stack trace cannot tell you the line number. I did
                              mention that before, and it's still true. :)

                              If when you include symbols with your executable, you find that the
                              default implementation of Exception.ToStr ing() does what you want, I
                              suppose that's all you really need to know.

                              Pete

                              Comment

                              Working...