Trying to test the error handling, how to raise an error?

As a newby, switching from the old VB6 to Xojo, I’ m quite pleased with all the new possibilities. But right now I’m struggling in testing the error handling.

I use the Introspection method as proposed in the documentation, although slightly modified in the handling.
In a method, I have a Try … Catch block as follows:

Try
' some code

Catch err As RuntimeException
  If Not Globals.HandleException(err, CurrentMethodName) Then  ' Specifies location 'CurrentMethodName'.
    ' In case handling fails (return value is 'False').
    Raise err
  End If
  
End Try

The ‘HandleException’ function uses:

Var sMsg As String = Introspection.GetType(error).FullName + " in " + source
sMsg = "An exception of type " + sMsg + " was caught."

My question now is: I want to raise an error, just to test and see what happens. So, I’ve put before the ‘Catch’ a line with:

Raise New OutOfBoundsException

But when I run the program, it stops at this line instead of handling the error using ‘HandleException’.

What am I doing wrong, or is there an other way to raise an error? In VB6 it was just a matter of adding a line ‘Err.Raise 9999’ (for example).

You need to continue debugging (press Play) as by default you have 'Break on Exceptions"
image


Edit: you can temporarily change that if you wish to simulate your finished app running but is not advised for the normal debug cycle.

1 Like

You can certainly use:

Raise New RuntimeException( "Error Message" )
// or even:
Raise New RuntimeException( "Error Message", 9999 )

You can also do that with the sub classes, but again the message and error numbers are provided by you:

Raise New OutOfBoundsException( "Error Message" )
// or even:
Raise New OutOfBoundsException( "Error Message", 9999 )

Otherwise the default message is “” and the default error number is 0.

Another way of generating the error is to simply write bad code:

Var MyArray( 2 )
MyArray( 5 ) = "" // this will cause an OutOfBoundsException

Agh, that’s easy… Just press the ‘Resume’ button, works perfect. Thanks a lot.

Handling works a (fortunately) as expected.

Jaap

1 Like

Thanks Ian, I’ll keep it in mind if I want to create my own error types.