IDE debugging question

L

Larry Lard

I'm hoping there's a way to do this, because my present technique
wastes a lot of time.

Suppose I have this console app:

Module Module1

Sub Main()
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer = a \ b
End Sub

End Module

When I run this from within the IDE, obviously execution will halt with
a divide by zero exception when attempting to assign to c. The
dialogbox presented to me tells me the exception type, and 'Additional
information' which is in fact the exception's Message property. The
buttons I can click are Break and Continue. If I click Continue,
execution ends, which is fine.

If I click Break, I am now in debug mode in the IDE and can I have the
command line and all the usual debug tools, so I can (in this example)
examine b and see that it is indeed zero, and understand why I got my
exception.

However. In a more real-world example, some of the information that
would help debug would be *in the Exception that has just been thrown*.
My question therefore is:

Once I have clicked Break and entered debug mode, is there any way to
obtain / interrogate / anything! the Exception which got me here?
 
P

Phil

Larry,

Sorry if I miss your point here. Can you not use the Try Catch block, and
have it show you the exception?

ie.

Try
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer = a \ b
Catch ex as exception
msgbox(ex.message)
End Try

Rgds,

Phil
 
L

Larry Lard

Sure, if I *had* a Try Catch block. But sometimes (and keep this one
just between us :)) I write code that isn't wrapped in a Try. Shocking,
I know. My present technique - the one which wastes time - is that when
I *do* get an unexpected exception, I then end execution, wrap the
relevant bit in a Try, then run again and follow the same code path to
get to that place, only this time I am waiting to Catch the exception.

I was hoping I could avoid having to make myself habitually put
everything in a Try block. I've changed coding practice in the past, so
it's possible - but I'd rather be able to just access the exception
that's made me break execution.
 
T

Tym

Sub Main()
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer = a \ b
End Sub

What's wrong with


Sub Main()
On Error Goto Error_Trap
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer = a \ b

Exit Sub
Error_Trap:
Select Case Err.Number
Case 11
'Divide by Zero Error
Msgbox "You've tried to divide by Zero!"
'Obviously you can display any relevant information _
here about variables....
Error.Clear
Case Else
MsgBox Err.Number & ": " & Err.Description & " " _
& Err.Source
Err.Clear
End Select
End Sub

---


Tym

Please do not adjust your brain, there is a fault with reality
 
P

Phill. W

.. . .
However. In a more real-world example, some of the information that
would help debug would be *in the Exception that has just been
thrown*.

And that's exactly what you get handed to you in an Exception Handler.

Try
c = 1 / 0
Catch ex As DivideByZeroException
MsgBox( ex.Message )
Catch ex As Exception
MsgBox( "Did anyone get the number of that truck?" _
& vbCRLF & ex.ToString() _
)
End Try

HTH,
Phill W.
 

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

Similar Threads


Top