I am a bit confused about error handling in Rebol. It has THROW and CATCH constructs:
>> repeat x 10 [if x = 9 [throw "Nine!"]]
** Throw error: no catch for throw: make error! 2
>> catch [repeat x 10 [if x = 9 [throw "Nine!"]]]
== "Nine!"
But this cast and trap are not related to the handler passed to the TRY refinement / PICTURE:
>> try/except [throw "Nine!"] [print "Exception handled!"]
** Throw error: no catch for throw: make error! 2
This is very specific to the Rebol error type:
>> try/except [print 1 / 0] [print "Error handled!"]
Error handled!
If you want, you can initiate your own errors, but do not use THROW, as in other languages. Discard errors will simply lead to a complaint that they were not caught, like any other type of value:
>> try/except [throw make error! "Error string"] [print "Error handled!"]
** Throw error: no catch for throw: make error! 2
You must do this in order to make the evaluator try and execute something like an error! call what he considers an βexceptionβ:
>> try/except [do make error! "Error string"] [print "Error handled!"]
Error handled!
(Note. You can use pre-made errors, for example cause-error 'Script 'invalid-type function!- see system/catalog/errorsfor more).
//, . , :
>> probe try [do make error! "Some error"]
make error! [
code: 800
type: 'User
id: 'message
arg1: "some error"
arg2: none
arg3: none
near: none
where: none
]
** User error: "Some error"
>> probe try [make error! "Some error"]
make error! [
code: 800
type: 'User
id: 'message
arg1: "Some error"
arg2: none
arg3: none
near: none
where: none
]
** User error: "Some error"
, CATCH . , "named throws":
>> code: [repeat x 10 [if x = 9 [throw/name "Nine!" 'number]]]
>> catch/name [do code] 'number
== "Nine!"
>> catch/name [do code] 'somethingelse
** Throw error: no catch for throw: make error! 2
, :