Home All Groups Group Topic Archive Search About

Headache about Error Handling and Debugging

Author
24 Feb 2006 6:39 PM
James Ma
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

Author
24 Feb 2006 6:53 PM
Kerry Moorman
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


Show quoteHide quote
"James Ma" wrote:

> 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
>
Author
24 Feb 2006 7:00 PM
James Ma
Thank you so much. It's working, though I'm using VS2005.

Show quoteHide quote
"Kerry Moorman" wrote:

> 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
>
>
> "James Ma" wrote:
>
> > 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
> >
Author
24 Feb 2006 8:41 PM
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
Author
25 Feb 2006 11:03 AM
CMM
Wow! Thanks! This is truly an amazing tip.
I love MS.

--
-C. Moya
www.cmoya.com
Author
25 Feb 2006 5:03 AM
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.


Show quoteHide quote
"James Ma" <Jame***@discussions.microsoft.com> wrote in message
news:AABD1846-9ADA-4256-B9C1-89593A0EC074@microsoft.com...
> 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
>