The difference between "errors / exceptions" and "throw / trick"?

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

, :

  • ? , THROW CATCH DO TRY/EXCEPT?

  • /EXCEPT /ON -ERROR - ?

  • "no catch for throw: make error! 2" - ? make error! 2?

+4
3

" ? , THROW, CATCH DO TRY/EXCEPT?"

, THROW CATCH ( ), - . THROW CATCH - , , .

, THROW CATCH, , " ", , .

Rebol error!, , .

, : , error!. , , THROW/CATCH.

:

  • Rebol , " " " ", .
  • , THROW/CATCH " Rebol" ( @HostileFork ), .

" "

, , Lisp. :

" " : ! 2 " - ? make make? 2?"

. ( !) , ​​ ( "no catch for throw" ) , make error! 2 ( ).

"/ /ON -ERROR - ?"

/EXCEPT . , , SO Q & A ​​ .

+5

, , goto. - .. continue

. is-there-an-equivalent-to-continue

+2

Rebol, , , . try/catch . , , , , . , , - , , , .

Sorry, I may not have answered your question directly, but I thought I might turn to your second question: should it be called ON-ERROR. I would say no, because an exception does not necessarily mean an error; as a means of controlling the flow, this is just a more general instruction about the occurrence of a condition that should not allow further execution along a certain path.

+2
source

Source: https://habr.com/ru/post/1546006/


All Articles