Using the %lasterror Variable

<< Click to Display Table of Contents >>

Navigation:  Robo-FTP User's Guide > Script Programming > Internal Script Variables >

Using the %lasterror Variable


The %lasterror variable is initialized to the last error or completion code of the most recently completed script command. The corresponding error text is stored in the %lasterrormsg internal variable.


Some commands do not change the value of %lasterror when they complete successfully. These commands include DISPLAY, SET and conditional branching commands like IFERROR. This exception allows you to add robust error handling logic to your command scripts.


The FTPSETERROR command sets the value of %lasterror to the numeric portion of the value of %ftplastresult (the last response from the remote server.)


If you want to use this value to report an error via email it is necessary to save the value in a script file variable so that it doesn't get reset by the results of a subsequent command. In the following example, the value of %lasterror is stored in a variable named err_num for use later in the body of an email message.


RCVFILE "daily_transactions.xml"


SET err_num = %lasterror

SET err_msg = %lasterrormsg

SET from = "Robo-FTP Service"  

SET email = ""

SET subj = "Automated download failed! (do not reply)"

SET body = "The following error was reported: "

SET body = body + "[" + err_num + "] " + err_msg

SET body = body + %crlf + "The command that caused this error was: " + %lastfailedcmd

SET attach = ""

CREATEMAIL from email subj body attach

SENDMAIL "" "Help Desk" ""

GOTO done




Related command(s): IFERROR, FTPSETERROR

See also: %lasterrormsg, Internal Script Variables, Script File Result Codes, Interactive Debugging, Simple Error Handling Example