Try/Catch Pragma Not Working in Debug Mode

When I am debugging my app even though I am using a try catch and a pragma the debugger still stops on exception and I have to press resume.

I am using this code at the top of my method:

 #Pragma BreakOnExceptions Off

This at the bottom of the method:

Exception err ErrorType (err, CurrentMethodName)

Here is the code in the method that stops the debugger on exception:

[code]
Try

    ' When my code has an OutOfBoundsException Here it stops the debugger!
    Select Case Test(0)
    Case 0     
   ' DO Stuff
    Case 1
   'DO Stuff etc...
   End Select

  Catch err As OutOfBoundsException
    WriteStatus("Error: OutOfBoundsException(ProcessResponse")

  End Try
  [/code]

Can anyone tell me what I am doing wrong and how I can prevent the debugger from stopping here? Thanks for any help…

huh
took your code
pasted it into a method in the IDE (more or less)
and hit run
works exactly as I expect

  
  #Pragma BreakOnExceptions Off
  
  Try    
    dim test(-1) as integer
    
    ' When my code has an OutOfBoundsException Here it stops the debugger!
    Select Case Test(0)
    Case 0     
      ' DO Stuff
    Case 1
      'DO Stuff etc...
    End Select
    
  Catch err As OutOfBoundsException
    break
    
  End Try
  
  
  Exception  err
    break
    

only stops on the break statements which is exactly what I expect
I even have break on exceptions enabled

removing the breaks it runs and never stops on the exception that is generated

@Norman Palardy Thank you for taking a look, when the debugger stops, it highlights this line of the code:

Select Case Test(0)

and I get the OutOfBoundsException information in the bottom panel. Am I doing something wrong? How do I prevent the debugger from stopping when this exception occurs? Thanks!

No idea whats different about what you’re doing vs what I posted as it does not stop here at all regardless of what setting the IDE’s Project > Break on Exceptions menu item is

That exact code never stops here

Ok thanks so much for taking a look at it I must be doing something wrong with the project setting. It’s been a long one.

if all that code is in a method it should not matter what the project setting is at all

thats exactly what I was seeing

The method that is populating the Test(0) array was originally throwing a NilObjectException.

I added this code to stop that issue.

[code] Try

'Do Stuff

Catch err As NilObjectException
WriteStatus(“Error: NilObjectException(decryptMsg)”)
End Try
[/code]

But for some reason I can’t add the Try/Catch to prevent the resulting OutOfBoundsException. I know I need to fix the underlying issue but I need the debugger to keep going so I can continue to test the applications connected to the Xojo app. Not sure why I can’t prevent it from stopping. :-/

To answer your question as asked:

  Try
    
   'Do Stuff
    
  Catch err As NilObjectException
    WriteStatus("Error: NilObjectException(decryptMsg)")

  Catch err As OutOfBoundsException
    // Do something
  End Try

But the right way to do it is:

if test.Ubound <> -1 then
  select case test(0)
  …
  end select
end if

I’d argue the “right way” will depend on many things
Personally I’d get in the habit of using assertions so you can make sure your code is passing in the things that methods expect etc

A simple global method something like

Sub assert(test as boolean, msg as string)
  if not test then
    msgbox msg
    break
  end if
End Sub

then wherever you can say “this has to be true” you can insert an assertion and make sure it is

IF in this code TEST should never be empty or nil when called then I’d write the first lines like

assert ( test <> nil, "test is nil but should not be")
assert ( test.ubound > 0, "test is empty but should not be")

  select case test(0)
  …
  end select

And note this makes your code slightly cleaner since you dont have it all buried in “if” tests
You can get a NIL array - but at least you can test against it

Function foo() As integer()
  return nil
End Function
  dim test() as integer = foo()
  
  assert ( test <> nil, "test is nil but should not be")
  assert ( test.ubound > 0, "test is empty but should not be")
  
  break

Thank you both for your help and the replies. I’ll give this all some thought and see if I can sort a solution.