Exceptions
All exceptions are fatal in CJam. Since output to STDERR is ignored by default, we can use this to our advantage.
All operator in CJam work by popping zero or more elements from the stack, perform some task and push zero or more elements on the stack. Exceptions occur while the task is performed, so this still pops the elements, but nothing is pushed in return.
Here are a few use cases:
Clearing a small stack
To clear a stack that contains two elements,
@
can be used.@
tries to pop three stack elements, but fails after popping the second.Any other operator that pops three elements would serve the same purpose.
Removing two or three elements from the stack
Any operator that is not implemented for these particular elements can be used to pop two or three elements from the stack right before exiting.
To pop two elements,
b
works if one of them is a character or none of them is an integer.To pop three elements,
t
works if none of the bottom-most two is an iterable, the bottom-most iterable is empty or none of them is an integer.Exiting from a loop
On occasions, we need to exit from a loop when an integer becomes zero or a string becomes too short. Rather than testing for these conditions, if the involved operations fail for zero, the empty string or singletons, we can simply let the program take its natural course.
Conditional execution
If the source code should not be executed for certain types of input, we sometimes can use an operator that fails that kind of input.
For example,
i
will fail for strings that do not evaluate to an integer andew
will fail for strings of length 0 or 1.