none
Try ... Catch ... there is NO way to determine the line of code that triggered the error? RRS feed

  • Question

  • This is something that I've never been able to resolve in any version of Visual Studio (Ultimate 2012 currently).  I'm using VB.NET but the Try ... Catch exception handling does NOT provide any information on exactly what line of code triggered the error when I'm in a debug session.  It'll tell me what the exception issue, but NOTHING about what line actually triggered the exception.

    If I have several 100's of lines of code in a Try ... Catch, this becomes a huge issue ... in most cases I stop execution and but a break point at the first line of code in the Try ... Catch and re-run my debug session (definitely NOT efficient).  I have to look for hints based on inspecting all the variables/structures.  This is TIME consuming and not helping the speed of my development work.

    Many years ago it was easy to get to the offending line of code ... before the invention of .NET and Try ... Catch, but now I seem to have lost that ability, such a basic and fundamental ability.  I've gone on for years just dealing with the lack of functionality in VS 2008+ series.  

    I hoping I'm wrong and there is a way that I just never discovered all these years, if so, can someone please tell me how?

    I've seen lots of silly suggestions such as put a Try ... catch on every line of code, but that's not productive.

    So have I really lost a fundamental debugging feature?  If so, how is this progress?

    Rob

     

    Thursday, July 23, 2015 7:17 PM

Answers

  • Couple of issues with your suggestion:

    1.  When the exception happens I will be located on the Catch ex as Exception, at this point the line number reference is the line of the Catch ex As Exception NOT the actual line that triggered the exception.

    2.  ex.StackTrace only references the container Method name, it will NOT tell me the line of code within the Method.  In order to have a useful line number in the ex.StackTrace I would need have a Try ... Catch on every single line of code in my Method (just not practical).

    So unfortunately your suggestions doesn't really solve the issue.

    Rob.

    That is not what I see. Here is my code with line numbers.

    And here is the StackTrace which identifies line 5 as the one where the exception was thrown. Line 5 is the statement with the problem, it is not the Catch statement.

    Friday, July 24, 2015 10:06 PM
  • I second that.

    The only time it shows line number but not the actual location generating the exception is that, when an exception is caught and re-thrown inside your code.

    Sunday, July 26, 2015 3:19 PM
    Answerer

All replies

  • If you display the StackTrace from the exception, it will include the file name and line number of the statement that threw the exception. For example:

    Sub Button2_Click(sender As Object, e As EventArgs)
    	Dim test As New List(Of String)
    	Try
    		MessageBox.Show(test(1))
    	Catch ex As Exception
    		MessageBox.Show(ex.StackTrace)
    	End Try
    End Sub

    Thursday, July 23, 2015 7:34 PM
  • In Visual Studio if you have the debugger attached you can also have the debugger break in your code execution as soon as it encounters an exception. You can even specify whether to break if its handled or not handled in a catch. This is very useful when you are debugging your code on your local machine.

    • To access the option go to the Debug menu and click on menu item Exceptions... 
    • To turn it on select Thrown and User-unhandled for group Common Language Runtime Exceptions
    • You can also turn exception types on/off for the debugger to stop on which is useful if you have some expected exceptions that are handled here/there.



    Mark as answer or vote as helpful if you find it useful | Igor

    Thursday, July 23, 2015 7:51 PM
  • As additional note, no line number if you're not placed the corresponding PDB file at the side of error throwing DLL/EXE file.

    And btw, I usually will just dump the output of ex.ToString() so it includes both ex.Message and ex.StackTrace.


    Friday, July 24, 2015 1:51 AM
    Answerer
  • Couple of issues with your suggestion:

    1.  When the exception happens I will be located on the Catch ex as Exception, at this point the line number reference is the line of the Catch ex As Exception NOT the actual line that triggered the exception.

    2.  ex.StackTrace only references the container Method name, it will NOT tell me the line of code within the Method.  In order to have a useful line number in the ex.StackTrace I would need have a Try ... Catch on every single line of code in my Method (just not practical).

    So unfortunately your suggestions doesn't really solve the issue.

    Rob.

    Friday, July 24, 2015 8:58 PM
  • Couple of issues with your suggestion:

    1.  When the exception happens I will be located on the Catch ex as Exception, at this point the line number reference is the line of the Catch ex As Exception NOT the actual line that triggered the exception.

    2.  ex.StackTrace only references the container Method name, it will NOT tell me the line of code within the Method.  In order to have a useful line number in the ex.StackTrace I would need have a Try ... Catch on every single line of code in my Method (just not practical).

    So unfortunately your suggestions doesn't really solve the issue.

    Rob.

    That is not what I see. Here is my code with line numbers.

    And here is the StackTrace which identifies line 5 as the one where the exception was thrown. Line 5 is the statement with the problem, it is not the Catch statement.

    Friday, July 24, 2015 10:06 PM
  • I second that.

    The only time it shows line number but not the actual location generating the exception is that, when an exception is caught and re-thrown inside your code.

    Sunday, July 26, 2015 3:19 PM
    Answerer
  • Hi,

    You can easily get the error line number using exception.tostring() function

    For example

    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Try
    			Dim x As Integer = "a" / 12
    		Catch ex As Exception
    			MsgBox(ex.ToString().Substring(ex.ToString().IndexOf("line")).Replace("line ", ""))
    		End Try
    	End Sub
    End Class

    Returns 4 which is the error line number

    Bill

    Wednesday, September 5, 2018 12:12 PM