Command: Exit from PUB/PRI method using abort status with optional return Value.
((PUB ┆ PRI))
ABORT [< Value] >
...
Returns: Either the current RESULT value, or Value if provided.
...
Code Block |
---|
if <bad condition>
abort 'If bad condition detected, abort |
...
Code Block |
---|
if <bad condition>
abort <value> 'If bad condition detected, abort with value |
...
To trap an ABORT, the call to the method or method chain that could potentially abort must be preceded with the Abort Trap symbol, a backslash ( \ ). For example, if a method named MayAbort
could possibly abort, or if it calls other methods that may abort, a calling method could trap this with the following:
Code Block |
---|
if \MayAbort 'Call MayAbort with abort trap abort <value> 'Process abort |
The type of exit that MayAbort actually used, ABORT or RETURN, is not automatically known by the trapping call; it may have just happened to be the destination of a RETURN command. Therefore, the code must be written in a way to detect which type was used.
...
Code Block |
---|
CON #0, None, Left, Right, Front, Back 'Direction Enumerations PUB Main | Direction Direction := None repeat case CheckSensors 'Get active sensor Left : Direction := Right 'Object on left? Let's go right Right : Direction := Left 'Object on right? Let's go left Front : Direction := Back 'Object in front? Let's go back Back : Direction := Front 'Object in back? Let's go front other : Direction := None 'Otherwise, stay still if not \Move(Direction) 'Move robot Beep 'We're stuck? Beep PUB Move(Direction) result := TRUE 'Assume success if Direction == None return 'Return if no direction repeat 1000 DriveMotors(Direction) 'Drive motor 1000 times PUB DriveMotors(Direction) <code to drive motors> if MotorStuck abort FALSE 'If motor is stuck, abort <more code> |
...
The Main
method gets sensor inputs and decides what direction to move the robot via the CASE statement. It then calls Move
in a special way, with the Abort Trap symbol, \
, preceding it. The Move
method sets its RESULT to TRUE and then calls DriveMotors in a finite loop. If it successfully completes, Move
returns TRUE. The DriveMotors
method handles the complication of moving the robot's motors to achieve the desired direction, but if it determines the motors are stuck, it cannot move them further and it aborts with a FALSE value. Otherwise it simply returns normally.
If everything is fine, the DriveMotors
method returns normally, the Move method carries on normally and eventually returns TRUE, and the Main method continues on normally. If, however, DriveMotors
finds a problem, it ABORTs which causes the Propeller to pop the call stack all the way through the Move method and up to the Main method where the Abort Trap was found. The Move method is completely oblivious to this and is now effectively terminated. The Main method checks the value returned by its call to Move (which is now the FALSE value that was actually returned by the aborted DriveMotors
method deep down the call stack) and it decides to Beep
as a result of the detected failure.
...