Headache about Error Handling and Debugging

G

Guest

Now I am debugging a buggy VB.NET function like following

Function foo()
Try
‘ many loops and codes and nested try catch here!
……
Catch (ex as Exception)
Msgbox ex.message
Finally
…
end try
End function

My headache is: when it causes an exception, it’s very hard for me to locate
which line causes the error. I remember if I use the “On Error Goto
Err_Hanlder†method, I can always use a simple “Resume†statement to help me
locate the buggy line. But I heard the VB’s “On Error GoTo†is not
recommended and also remember I can only use the â€try catch†in C#, C++ or
Java.

Question: Is there an easy method to achieve the same effect as the resume
(I mean for debugging purpose)? or can I set the IDE to break at the buggy
line instead of at the catch block? Is this a legitimate reason that favor
“On Error Goto†more than “Try Catch�

Look forward your insight.
James
 
G

Guest

James,

In VS 2003 you can break into the debugger when an exception is thrown.

From the Debug menu, choose Exceptions. From the Exceptions dialog select
Common Language Runtime Exceptions. Click the radio button to break into the
debugger when an exception is thrown.

Now when an exception is thrown the debugger will highlight the line causing
the exception, even though the code is within a Try Catch block.

Kerry Moorman
 
J

Jim Wooley

Now I am debugging a buggy VB.NET function like following
My headache is: when it causes an exception, it's very hard for me to
locate which line causes the error. I remember if I use the "On Error
Goto Err_Hanlder" method, I can always use a simple "Resume" statement
to help me locate the buggy line. But I heard the VB's "On Error GoTo"
is not recommended and also remember I can only use the "try catch" in
C#, C++ or Java.

Another alternative you can do is to add line numbers to your code and retrieve
the last successful line using the ERL() function (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/valrferlproperty.asp).
This function has been in VB but undocumented between versions 3 and 6. It
is now documented again.

See http://devauthority.com/blogs/jwooley/archive/2005/12/21/661.aspx for
a sample implementation.

Jim Wooley
 
S

Stephany Young

And another alternative is, instead of displaying the exception message in a
message box, write the exception itself to the console:

Console.Writeline(ex.ToString())

This will give you the stack trace which in turn will show you the name of
the source file and the line number along with the procedure name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top