none
Try, Catch and line number

    Question

  • Hi all,

    I have an question that is a little tricky to explain so I'll demonstrate it in code:

    1 Dim var As SomeObject  
    2 var = New SomeObject("someArgument")  
    3 var.ExecuteSomeMethod()  
    4 var.SomeProperty = "someValue" 

    If ExecuteSomeMethod causes an error and is not handled I can look at the event log and see the exact line (3) where the error occurred but if I put a try/catch around the code, I can no longer see the line number. If I have the following code for example, the event log stack trace will only show me where I threw the exception again.

    1 Try 
    2     Dim var As SomeObject  
    3     var = New SomeObject("someArgument")  
    4     var.ExecuteSomeMethod()  
    5     var.SomeProperty = "someValue" 
    6 Catch ex As Exception  
    7     'Do something to manage the result of the error  
    8     Throw 
    9 End Try 
    10  

    In the above example, this wouldn't be so much of a problem as the stack trace will tell me that ExecuteSomeMethod was involved. However, this becomes a real problem if I call ExecuteSomeMethod more than once as I am no longer able to see which call to ExecuteSomeMethod caused an exception and I then need to try and replicate the issue in the VS debugger.

    Any ideas as to how I can get the failing line number within the Try/Catch?

    Stephen
    Monday, June 02, 2008 9:30 AM

Answers

  • Use Catch without the catch clause.  It will preserve the stack trace.  For example:

    Module Module1
      Sub Main()
        Try
          Test()
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace)
        End Try
        Console.ReadLine()
      End Sub
      Sub Test()
        Try
          Test2()
        Catch
          Throw
        End Try
      End Sub
      Sub Test2()
        Throw New Exception("Test")
      End Sub
    End Module


    Hans Passant.
    • Marked as answer by Bruno Yu Friday, June 06, 2008 5:58 AM
    Tuesday, June 03, 2008 1:31 PM

All replies

  • You should probably use "Finally" instead if you are using Catch + Throw to do some cleanup.  You could also throw a new exception and use the original exception as the InnerException property of the new exception.   Tricky to get right though.
    Hans Passant.
    Monday, June 02, 2008 12:39 PM
  • Thanks for the reply.

    I was only giving an example there. The most common thing for me to do in a Catch is to roll back a transaction, which I wouldn't do in a Finally.

    So, if I throw a new exception with the caught exception as the inner exception, I should be able to get the offending line in the current context?

    Stephen
    Tuesday, June 03, 2008 12:37 PM
  • Use Catch without the catch clause.  It will preserve the stack trace.  For example:

    Module Module1
      Sub Main()
        Try
          Test()
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace)
        End Try
        Console.ReadLine()
      End Sub
      Sub Test()
        Try
          Test2()
        Catch
          Throw
        End Try
      End Sub
      Sub Test2()
        Throw New Exception("Test")
      End Sub
    End Module


    Hans Passant.
    • Marked as answer by Bruno Yu Friday, June 06, 2008 5:58 AM
    Tuesday, June 03, 2008 1:31 PM
  • Thank you very much! Just what I was after.

    Sorry its taken so long to reply. I keep forgetting to turn alerts on.

    Stephen
    Friday, July 04, 2008 8:25 AM