Why does Real Studio Break on the Catch of an Exception?

746 views Asked by At

I have a try-catch block like this:

Try
  Listbox1.RemoveRow(Listbox1.ListIndex)
Catch err As OutOfBoundsException
  MsgBox("Derp")
End Try

When I run my project in the debugger I get an OutOfBoundsException on the exact line I was trying to catch! Why doesn't this work?!?

2

There are 2 answers

0
Ammar On BEST ANSWER

Seems to me like the debugger will break at that line and show you the exception. But if you hit resume, it will continue, catch the exception, and then display the message.

Maybe they changed the behavior of the debugger with this release.

Update: You can go to Project > Break on exception to change this

0
Andrew Lambert On

The debugger will break as soon as the exception is encountered, before any other code gets executed. This includes any exception handling code you may have put in like a Try...Catch block.

If you have a bit of code that raises a lot of exceptions and you'd rather not have to step through it every single time you debug, you have two options: nuclear and surgical.

The nuclear option is to tell the debugger to NOT break on any exceptions at all, which has the unfortunate side effect of applying to your entire project instead of the small portion of it you're excepting on.

The surgical option is to use pragma directives to toggle breaking on exceptions off and on around the troublesome code:

#Pragma BreakOnExceptions Off
try
  Listbox1.RemoveRow Listbox1.ListIndex
catch err As OutOfBoundsException
  MsgBox "Derp"
End
#Pragma BreakOnExceptions On

This is much more preferable then simply turning off part of the debugger altogether. Note: the BreakOnExepctions directive will revert to you global setting (on or off) as soon as the function returns and is local to the code it surrounds.