Catch exception, show properties
The following simple example divides 42 by 0, which, of course, will throw an error.
In the case of an error, the error handler (on error goto failed
and failed:
) will then print the Err
object's properties:
option explicit
sub main() ' {
debug.print ("25/5 = " & divide(25, 5))
debug.print ("42/0 = " & divide(42, 0))
end sub ' }
function divide(a as double, b as double) as double ' {
on error goto failed
divide = a / b
exit function
failed:
debug.print("division failed")
debug.print(" err.description = " & err.description)
debug.print(" err.number = " & err.number )
debug.print(" err.source = " & err.source )
end function ' }
Raising a user error
err.raise errorNumber, source, description, helpFile, helpContext
All parameters except
errorNumber
are
optional.
option explicit
sub main() ' {
on error goto failed
debug.print("42/6 = " & divide(42, 6))
debug.print("55/0 = " & divide(55, 0))
debug.print("13/9 = " & divide(13, 9))
failed:
debug.print("Failed: " & err.description)
end sub ' }
function divide(a as double, b as double) as double ' {
if b = 0 then
call err.raise(1000, "divide", "b must not be zero")
end if
divide = a / b
end function ' }