For Beginners

Lesson 23/23

Try Again

If you take a look at various commands’ description, you will notice that a vast majority of them share the same arguments. Four of these arguments are targeted at error handling: errorcall, errorjump, errormessage and errorresult. You can use one of them to convince a user to provide a correct input.

You will use the try command, which is another block command: it tries to execute everything inside the block between try and end keywords. In case an error occurs within this block, the robot will do what one of the error... arguments instructs: call a procedure, jump to a label, display a message or just store the error information in a variable.

Try this out:

label repeat
try errorjump repeat
  dialog.ask ‴Enter a digit (0-9):‴ result ♥input
  ♥input = ⟦integer⟧♥input

The tryend block asks a user to enter a digit in a dialog box. This digit is stored in the ♥input variable. Then, the robot tries to force the integer type on the provided value. If it fails to do so, because a user entered non-numerical characters, an error occurs. And this is the moment, when the errorjump argument for the try command steps in: it tells the robot to go back to the repeat label, thus starting the tryend block again in hope for a correct user input. This will be repeated over and over, until a user provides digits and the ♥input variable is successfully converted into an integer.