Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Don't understand "try" and "disarm"

From: swhite:ci:bloomington:mn:us at: 14-Mar-2006 9:33

Really, it is quite amazing to post a programming question and get an answer from someone who is (it seems) half-way around the world. I don't always get and answer as quickly from the next desk. I have used the tips I have received and have isolated my problem in the following test script: ;;;;; start of test script REBOL [ ] TRANSFER-FILE: does [ either error? FTP-RESULT: try [ ;;;; read %nonexistent.txt ;;;;; this file does NOT exist ;;;; read %fileexists.txt ;;;;; This file DOES exist write %tempfile.txt read %fileexists.txt ] ;;;;;; -- There was some error [ FTP-ERROR-OBJECT: disarm FTP-RESULT FTP-ERROR-MESSAGE: rejoin [ "The file was not transferred." " The REBOL interpreter returned the following message: " to-string FTP-ERROR-OBJECT/id ] alert FTP-ERROR-MESSAGE ] ;;;;;; -- There was no error [ alert "The file was read successfully" ] ] view layout [ title "Test the script" button "GO" [TRANSFER-FILE] ] ;;;;; end of test script Here is the problem: In the "try" block I have three different operations, (1) reading a file that does not exist, (2) reading a file that does exist, and (3) reading a file that exists and writing it to a new file. I comment-out two of the three in various combinations to find out what does not work. If I read a file that does not exist, the script works. If I read a file that does exist, the script works. If I use the third test, reading a file and then writing it, the script crashes with the following message:
>> do %trytest.r
** Script Error: FTP-RESULT needs a value ** Where: TRANSFER-FILE ** Near: either error? FTP-RESULT: try [ write %tempfile.txt read %fileexists.txt ]
Can anyone explain why this happens? Thank you.
>>> 3/14/2006 8:03 AM >>>
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-