Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Error Line Numbers

  1. #1

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Resolved [RESOLVED] [2005] Error Line Numbers

    Is there any way to get the actual line number that an error occurs on in conjunction with the Throw statement? Here is a sample:

    VB Code:
    1. Private Sub TestErrors()
    2.  
    3.         Try
    4.  
    5.             Dim i As Integer
    6.  
    7.             i = 5
    8.  
    9.             i *= 3
    10.  
    11.             CreateError(i)
    12.  
    13.         Catch ex As Exception
    14.             DisplayException(ex)
    15.         End Try
    16.  
    17.     End Sub
    18.  
    19.     Private Sub CreateError(ByVal i As Integer)
    20.  
    21.         Try
    22.  
    23.             Dim n As Integer
    24.  
    25.             n = CInt(i / n)
    26.  
    27.         Catch ex As Exception
    28.             Throw ex
    29.         End Try
    30.  
    31.     End Sub

    In this example, the error stack will report lines 11 and 28 as the error line numbers. I really want to know that line 25 is the "real" root of the error and that line 28 is just where the error was thrown. If I were to handle the exception rather than use a throw, then the stack would report line 25. The problem is, that I need to use throw in many of my objects to allow the calling application to handle exceptions. Any ideas or thoughts?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Error Line Numbers

    I'm curious as to why the line number is important? If the code was altered, the line number would change, so it is only a reasonably constant pointer in well settled code. Naturally, the example you posted is a trivial simplification of the real issue you are interested in, but I wonder if that doesn't illustrate the issue well. The only place I can think that the line number would be particularly important is if the calling app was a test suite testing the called code. Otherwise, either the type of exception or the fact of an exception, would be relevant, but the exact location would not.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: [2005] Error Line Numbers

    Good question. When an error occurs in our application, we have an exception object that handles the exception and displays a user friendly dialog to the user. If the problem persists, they can report the error to our support team and embedded in that information is the stack trace of the error. The line number will tell us the exact line the error occurred on. Make sense?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Error Line Numbers

    Yes. The result is that your reporting system is effectively the module testing that I mentioned above, you are simply using the end users to test for bugs. That almost sounds mean, but in fact it is the model my agency is using explicitly. Unless you have a very extensive testing structure, the end users will always find bugs.

    I have not tried this, so the following suggestion is as likely to be meaningless as meaningful, but here it is: In VB6 you had the ability to create custom errors. In .NET, you can create custom derivations of the Exception class. In your example, you simply re-throw the exception that got you to the first handler. I would suggest that you make a custom exception that contains the line number or stack trace, and throw that instead. Basically, this would mean having some canned routine (I would expect it to be standard code that could be cut and pasted) that can be pasted into each exception handler that takes the exception in the inner catch, creates a new exception of the custom type with the relevant information contained in something (like the message, tag, or other property), then throws that custom exception.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: [2005] Error Line Numbers

    Hmmm, interesting approach. Any ideas as to why the "root" line number is lost or to why I receive no line number sometimes?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Error Line Numbers

    The exception class should have a stack trace. However, once you handle the exception, then re-throw, I expect that the system is treating the re-throw as a whole different exception distinct from the original one, despite the fact that you are really just re-throwing. I would guess that's some kind of internal counter issue, but I have never really looked into it.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: [2005] Error Line Numbers

    Ah, very interesting...kind of annpying, but your explanation makes sense. Thanks!

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Error Line Numbers

    Quote Originally Posted by Shaggy Hiker
    I expect that the system is treating the re-throw as a whole different exception distinct from the original one, despite the fact that you are really just re-throwing. I would guess that's some kind of internal counter issue, but I have never really looked into it.
    that is true in this case
    vb Code:
    1. Private Sub CreateError(ByVal i As Integer)
    2.          Try
    3.              Dim n As Integer
    4.              n = CInt(i / n)
    5.         Catch ex As Exception
    6.             Throw ex
    7.         End Try
    8.      End Sub

    But you can preserve the original exception by doing this
    vb Code:
    1. Private Sub CreateError(ByVal i As Integer)
    2.          Try
    3.              Dim n As Integer
    4.              n = CInt(i / n)
    5.         Catch ex As Exception
    6.             Throw
    7.         End Try
    8.      End Sub

  9. #9

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: [2005] Error Line Numbers

    The results appear to be the same whether I use Throw versus Throw ex. Not really sure what the difference is as both report the same error stack.
    Last edited by clarkgriswald; Nov 12th, 2007 at 04:32 PM.

  10. #10
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: [2005] Error Line Numbers

    My stupid suggestion would be to remove the Try/Catch block from the CreateError method since it only serves to re-throw the exception. BTW, I think that CLR is correct in reporting line 28 as the one that caused the error since you caught it with the Catch block. The stack trace of the exception will probably not be useful since it includes a list of code locations that have thrown an exception.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  11. #11

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: [2005] Error Line Numbers

    Removing the Try/Catch block is not good coding practice so I do not want to do that (I'm sure there are also other reasons that this is not a good idea). As I had mentioned, if I handle the error in the initial Catch statement, the error line number will be 25, which is what I want. I want the line the error occurred, not the line that threw the error.

    There has to be some explanation for this, I'm just not getting it.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Error Line Numbers

    You should create a new exception object and assign the original exception to its InnerException property. The InnerException will then have the original line number. To see what I mean, try this code:
    Code:
    Private Sub Method1()
        Try
            Me.Method2()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    
    Private Sub Method2()
        Try
            IO.File.Open("This file does not exist", IO.FileMode.Open)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
            Throw
        End Try
    End Sub
    You'll see that the first message has the original line number but the second message has the line number of where the exception was rethrown. Now try this code:
    Code:
    Private Sub Method1()
        Try
            Me.Method2()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    
    Private Sub Method2()
        Try
            IO.File.Open("This file does not exist", IO.FileMode.Open)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
            Throw New Exception("Outer Exception", ex)
        End Try
    End Sub
    You''l see that the second message still shows the line number of where the new exception was thrown but it also includes the stack trace for the inner exception, which includes the original line number.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: [2005] Error Line Numbers

    I agree with the above. This is what the inner exception is meant for.
    Also note that line numbers will only appear if the pdb is distributed in the release environment. no pdb means no line numbers since the numbers are coming from the debug info in the pdb file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width