[REBOL] Re: Don't understand "try" and "disarm"
From: petr:krenzelok:trz:cz at: 14-Mar-2006 15:03
Hi Steven,
a bit confusing, isn't it? In fact, some time ago, I just did the same
mistake. I expected 'try to catch the error. Well, it does so, but it
returns it. I don't like the way it work, but it is so, so let's just
find some way out from it -
->> try [read %nonexistant.txt]
** Access Error: Cannot open /c/rebol/view/nonexistant.txt
** Near: read %nonexistant.txt
->> error? try [read %nonexistant.txt]
== true
You can see? 'try is there just to catch the error, but you still have
to somehow maintain it. Single 'error? is enough for script not to
crash, let's rewrite it a bit:
->> if error? try [read %nonexistant.txt][print "file does not exist"]
file does not exist
It seems to me, the error is returned from 'try call to the interpreter.
We can "catch" it using 'disarm function:
->> probe disarm try [read %nonexistant.txt]
make object! [
code: 500
type: 'access
id: 'cannot-open
arg1: "/c/rebol/view/nonexistant.txt"
arg2: none
arg3: none
near: [read %nonexistant.txt]
where: none
]
I think you was not alone, who was a bit confused, so we've got some
shortcuts in some later Core releases - 'attempt function:
->> attempt [read %nonexistant.txt]
== none
Attempt is a mezzanine function (defined in rebol):
->> source attempt
attempt: func [
{Tries to evaluate and returns result or NONE on error.}
value
][
if not error? set/any 'value try :value [get/any 'value]
]
I hope you can proceed from that further ....
Cheers,
-pekr-