[REBOL] Re: Help me, Obi Reb Kenobi, you're my only hope!
From: carl:cybercraft at: 29-Aug-2002 20:23
On 29-Aug-02, Ed Dana wrote:
> Carl Read wrote:
>> Hee - wasted time on this myself today!
>>
>> The problem is that 'print doesn't return a value when 'Fred is
>> expecting one...
>>
>>>> Fred: print "What's wrong with this?"
>>>>
>> What's wrong with this?
>> ** Script Error: Fred needs a value
>> ** Near: Fred: print "What's wrong with this?"
>>
>> and it's not trapped by your error routine as 'Fred is outside the
>> block that's being 'try'ed. Place 'Fred inside it and you'll trap
>> the error...
>>
>>>> If Error? try [Fred: Print "See?"][Print "Whatever!"]
>>>>
>> See?
>> Whatever!
>>
> Yes. But the value that I was trying to assign Fred was the error
> that was to occur when I attempted my action.
Ah. And thanks to the other responses to your post, (specifically the
ones talking about unset), this might be what you want...
if error? Fred: try [unset? print "OK!"][
Fred: disarm Fred print "Fred reports..." probe Fred
]
That gives...
OK!
== none
and this...
if error? Fred: try [unset? 1 / 0][
Fred: disarm Fred print "Fred reports..." probe Fred
]
Fred reports...
make object! [
code: 400
type: 'math
id: 'zero-divide
arg1: none
arg2: none
arg3: none
near: [unset? 1 / 0]
where: none
]
Okay - a full blown test...
test: [either error? Fred: try [
unset? Harry: switch random 3 [
1 [print "Printing..."]
2 ["Returning a value..."]
3 [1 / 0]
]
][
Fred: disarm Fred
print "Fred reports error near..." print mold Fred/near
][
print ["Harry reports..." Harry]
]]
Doing that block 10 times gives...
>> loop 10 [do test]
Harry reports... Returning a value...
Printing...
Fred reports error near...
[unset? Harry: switch random 3]
Fred reports error near...
[1 / 0]
Harry reports... Returning a value...
Printing...
Fred reports error near...
[unset? Harry: switch random 3]
Fred reports error near...
[1 / 0]
Printing...
Fred reports error near...
[unset? Harry: switch random 3]
Fred reports error near...
[1 / 0]
Harry reports... Returning a value...
Printing...
Fred reports error near...
[unset? Harry: switch random 3]
> More accurately, I have written a couple of daemons, that execute a
> command that I pass to it as a parameter. I wanted the daemon to
> send out an email if it could not execute the action it was
> assigned.
> Something like this (where Action is the variable that holds the
> statement to be executed:
> If Error? Error: Try [ Do Action] [ Send Warning email ]
Hopefully the above is what you need. If not, keep asking. Someone
here will know the answer!
> Realizing that Try is similar to Do, I even used:
> If Error? Error: Try [ Action] [ Send Warning email ]
> The first executed without issue, making me think that everything
> was executing OK; it wasn't. The second gave me the "no value"
> error.
> But this is why I'm confused: If try is to be used to help trap
> errors, why does it care if a value is set or not?
It doesn't - your error was outside the 'try block...
>> either error? try [print "x"]["Error!"]["No error!"]
x
== "No error!"
>> either error? Fred: try [print "x"]["Error!"]["No error!"]
x
** Script Error: Fred needs a value
** Near: either error? Fred: try [print "x"]
There'd have to be a special case made to stop 'try causing an error
in such cases - and yes, there probably should be.
Writing the above made me think about an error-trap within an
error-trap. So, an alternative to the first test...
test2: [
either error? try [
if error? Fred: try [
switch random 3 [
1 [print "Printing..."]
2 ["Returning something..."]
3 [1 / 0]
]
][
print "Error!" Fred: disarm Fred
]
][
print "Fred Error!" ; ie, Fred unset.
][
print ["Fred reports..." probe Fred]
]
]
Not the same output as the first test though...
>> loop 10 [do test2]
Returning something...
Fred reports... Returning something...
Returning something...
Fred reports... Returning something...
Printing...
Fred Error!
Printing...
Fred Error!
Returning something...
Fred reports... Returning something...
Error!
make object! [
code: 400
type: 'math
id: 'zero-divide
arg1: none
arg2: none
arg3: none
near: [1 / 0]
where: 'switch
]
Fred reports... ?object?
Printing...
Fred Error!
Returning something...
Fred reports... Returning something...
Returning something...
Fred reports... Returning something...
Returning something...
Fred reports... Returning something...
--
Carl Read