Throw without parameter...

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

    #1

    Throw without parameter...


    Hi,

    If you use just throw without parameter should preserve the complete stack
    trace and the exception information.

    for example:

    1. private void Bar()
    2. {
    3. try
    4. {
    5. string s = null;
    6. if (s.Length == 0)
    7. return;
    8. }
    9. catch
    10. {
    11. // do some processing...
    12. throw;
    13. }
    14. }
    15.
    16.private void Foo()
    17.{
    18. try
    19. {
    20. Bar();
    21. }
    22. catch (Exception ex)
    23. {
    24. // do some processing...
    25. Console.WriteLi ne(ex.ToString( ))
    26. }
    27.}

    In the above code it should show error line nos 6, 12 and 20 in the stack
    trace. But now in newer versions it is just showing the line nos. 12 and 20.

    It seems this functionality is removed or a bug in newer versions. It's no
    longer preserving the stack trace info. I saw this In .NET l.0 framework
    (VS.NET 2002).


    Regards,
    Ashok


  • Alberto Poblacion

    #2
    Re: Throw without parameter...

    "AshokG" <gw2ksoft@hotma il.comwrote in message
    news:OefNi3B$HH A.1188@TK2MSFTN GP04.phx.gbl...
    If you use just throw without parameter should preserve the complete stack
    trace and the exception information.
    [...] In the above code it should show error line nos 6, 12 and 20 in the
    stack trace. But now in newer versions it is just showing the line nos. 12
    and 20.
    It could be that the optimizer in the compiler is inlining the function
    that you are calling. Therefore, there is no longer a "line 6" -- the
    function that contained line 6 has moved into line 12.


    Comment

    • RB

      #3
      Re: Throw without parameter...

      AshokG wrote:
      Hi,
      >
      If you use just throw without parameter should preserve the complete stack
      trace and the exception information.
      >
      for example:
      >
      1. private void Bar()
      2. {
      3. try
      4. {
      5. string s = null;
      6. if (s.Length == 0)
      7. return;
      8. }
      9. catch
      10. {
      11. // do some processing...
      12. throw;
      13. }
      14. }
      15.
      16.private void Foo()
      17.{
      18. try
      19. {
      20. Bar();
      21. }
      22. catch (Exception ex)
      23. {
      24. // do some processing...
      25. Console.WriteLi ne(ex.ToString( ))
      26. }
      27.}
      >
      In the above code it should show error line nos 6, 12 and 20 in the stack
      trace. But now in newer versions it is just showing the line nos. 12 and 20.
      >
      It seems this functionality is removed or a bug in newer versions. It's no
      longer preserving the stack trace info. I saw this In .NET l.0 framework
      (VS.NET 2002).
      >
      >
      Regards,
      Ashok
      >
      >
      Java would certainly preserve the stack trace, but I've never known .NET
      to do that.

      In .NET I tend to use nested exceptions for this functionality as it
      will print out the stack trace of the caught exception, and all nested
      exceptions.

      So, your line 9 -13 becomes:

      catch (Exception ex)
      {
      throw new Exception("I'm nesting an exception", ex)
      }

      Cheers,

      RB.

      Comment

      • Frans Bouma [C# MVP]

        #4
        Re: Throw without parameter...

        AshokG wrote:
        >
        Hi,
        >
        If you use just throw without parameter should preserve the complete
        stack trace and the exception information.
        >
        for example:
        >
        1. private void Bar()
        2. {
        3. try
        4. {
        5. string s = null;
        6. if (s.Length == 0)
        7. return;
        8. }
        9. catch
        10. {
        11. // do some processing...
        12. throw;
        13. }
        14. }
        15.
        16.private void Foo()
        17.{
        18. try
        19. {
        20. Bar();
        21. }
        22. catch (Exception ex)
        23. {
        24. // do some processing...
        25. Console.WriteLi ne(ex.ToString( ))
        26. }
        27.}
        >
        In the above code it should show error line nos 6, 12 and 20 in the
        stack trace. But now in newer versions it is just showing the line
        nos. 12 and 20.
        >
        It seems this functionality is removed or a bug in newer versions.
        It's no longer preserving the stack trace info. I saw this In .NET
        l.0 framework (VS.NET 2002).
        Isn't there an inner exception in 'ex' in Foo ?

        FB

        --
        ------------------------------------------------------------------------
        Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
        LLBLGen Pro website: http://www.llblgen.com
        My .NET blog: http://weblogs.asp.net/fbouma
        Microsoft MVP (C#)
        ------------------------------------------------------------------------

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Throw without parameter...

          What a dirty programming style

          Cor


          "AshokG" <gw2ksoft@hotma il.comschreef in bericht
          news:OefNi3B$HH A.1188@TK2MSFTN GP04.phx.gbl...
          >
          Hi,
          >
          If you use just throw without parameter should preserve the complete stack
          trace and the exception information.
          >
          for example:
          >
          1. private void Bar()
          2. {
          3. try
          4. {
          5. string s = null;
          6. if (s.Length == 0)
          7. return;
          8. }
          9. catch
          10. {
          11. // do some processing...
          12. throw;
          13. }
          14. }
          15.
          16.private void Foo()
          17.{
          18. try
          19. {
          20. Bar();
          21. }
          22. catch (Exception ex)
          23. {
          24. // do some processing...
          25. Console.WriteLi ne(ex.ToString( ))
          26. }
          27.}
          >
          In the above code it should show error line nos 6, 12 and 20 in the stack
          trace. But now in newer versions it is just showing the line nos. 12 and
          20.
          >
          It seems this functionality is removed or a bug in newer versions. It's no
          longer preserving the stack trace info. I saw this In .NET l.0 framework
          (VS.NET 2002).
          >
          >
          Regards,
          Ashok
          >
          >

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: Throw without parameter...

            Cor Ligthert[MVP] wrote:
            What a dirty programming style
            >
            Cor
            >
            I fail to see how that has anything at all to do with the question.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Throw without parameter...


              ">
              I fail to see how that has anything at all to do with the question.
              >
              I fail to see why you are sending this message, the code does not even
              compile in VB.Net.

              Cor

              Comment

              • Alberto Poblacion

                #8
                Re: Throw without parameter...

                "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                news:154329D6-9600-4FDF-918A-A4AEDF4429EA@mi crosoft.com...
                I fail to see why you are sending this message, the code does not even
                compile in VB.Net.
                That's one of the reasons why cross-posting to multiple groups is
                discouraged. The original poster sent the message (which contained some code
                in C#) to multiple groups, including the VB group, where of course the code
                won't even compile.

                Comment

                • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                  #9
                  Re: Throw without parameter...

                  Cor Ligthert[MVP] wrote:
                  >
                  ">
                  >I fail to see how that has anything at all to do with the question.
                  >>
                  I fail to see why you are sending this message, the code does not even
                  compile in VB.Net.
                  >
                  Cor
                  >
                  What code? I didn't post any code. You are not making any sense at all.

                  --
                  Göran Andersson
                  _____
                  Göran Anderssons privata hemsida.

                  Comment

                  Working...