开发者

Wrong line number on stacktrace exception

I've got some code which errors and I'm using the stacktrace to find out what the line number is but it seems to be giving me the wrong number

Here's my code

Try

    Dim query As String = "Select * from table"
    Dim ds As DataSet = data.db(query)

Catch e As Exception

    Dim st As New StackTrace(True) 'This is the line number it gives me'
    Dim sf As StackFrame = st.GetFrame(0)
    Response.Write(" Method: " & sf.GetMethod().Name)
    Response.Write(" Fil开发者_JAVA百科e: " & sf.GetFileName())
    Response.Write(" Line Number: " & sf.GetFileLineNumber().ToString())

End Try

It seems to give me the line number of where the StackTrace is starting rather than the line number of what is causing the exception

Any ideas?


If you particularly want a StackTrace object, and don't just want the string from e.StackTrace, then change your constructor call to;

Dim st As New StackTrace(e, True)

"st" will now be initialised with the source details as you're expecting.

See http://msdn.microsoft.com/en-us/library/dsay49kt.aspx for details.


Why don't you use the info from the Exception object?

Catch e As Exception

    Response.Write(e.StackTrace)
    Response.Write(" Method: " & e.TargetSite)
    Response.Write(" File: " & e.Source)

End Try


The StackTrace(true) constructor creates a new stacktrace for this point in the code.

Use the .Stacktrace property of the exception.


Surely you want the StackTrace of the Exception? You are creating a new stacktrace at the line of code in the exception handler.

You want e.StackTrace instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜