Exceptions - Can you detect the line number on the form?
Is it possible to find detect the line number on which an error occures on a form?
Code:
' yadda is the line number
Try
... stuff
Catch ex As Exception
MsgBox(ex.Message & " on line number " & yadda , MsgBoxStyle.Critical, "Run Time Error ... ")
Finally
Me.Close()
End Try
I've gotten sort of spoiled using PHP for web programming and run time errors are easy to correct because PHP returns the exact line number the error is on. Would be nice to be able to get vb.net to do this too.
Re: Exceptions - Can you detect the line number on the form?
Yes, use the ex.StackTrace to get the line number of the exception.
Re: Exceptions - Can you detect the line number on the form?
just keep in mind that stack trace will probably not give you info on the line number if you're in release mode;)
Re: Exceptions - Can you detect the line number on the form?
I looked it up and it doesnt mention anything about debug vs. release mode. Hmm... probably still shows.
Re: Exceptions - Can you detect the line number on the form?
I think I tried it once and it didnt work...
it "shouldnt" because compiled code doesn't understand what line numbers mean
I dont have access to Vb.net right now, but someone should give it a try
Re: Exceptions - Can you detect the line number on the form?
The stack trace will not give you line numbers because, as stated, there are no line numbers in compiled code. It will give you the the fully qualified name of the method in which the exception was thrown though, so how hard can it be to find?
Re: Exceptions - Can you detect the line number on the form?
It can be realy really really hard if you have a huge procedure. :lol:
Thanks for doing the verification John. :thumb:
Re: Exceptions - Can you detect the line number on the form?
Quote:
Originally Posted by RobDog888
It can be realy really really hard if you have a huge procedure.
That's quite true and I've experienced that myself, which is one of the reasons I have now sworn to always follow the "rule" of keeping methods short and farming out chunks to their own methods.
Re: Exceptions - Can you detect the line number on the form?
Now what would happen if you had a large procedure broken down into smaller procedure calls from the main on and the main one was the only one with a Try Catch? Would the StackTrace show one of the child sub procedures if the error was caused in it?
Re: Exceptions - Can you detect the line number on the form?
You'll note that the stack trace shown in my images includes the calls to the Framework itself, which shows that it goes right to the deepest point within the managed code. Unmanaged code is another story I think. For aditional proof se the attached image. Also note that line numbers are shown for the debug code but not for the Framework calls, which are release compiled.
Re: Exceptions - Can you detect the line number on the form?
Quote:
Originally Posted by jmcilhinney
The stack trace will not give you line numbers because, as stated, there are no line numbers in compiled code. It will give you the the fully qualified name of the method in which the exception was thrown though, so how hard can it be to find?
:) good point too