Common errors to watch for
Certain errors are common in scripts. The scripts in this section contain errors so that you can practice identifying and correcting error conditions. Becoming familiar with these error conditions helps you to more quickly identify and correct them.
Some of these examples cause the debugger to appear. If you are not yet familiar with using the debugger, just close it by double-clicking on its System menu.
Messages not understood
Script Debugger messages stating <anObject> does not understand <message> are probably the most common errors in scripts. The causes include:
•Misspelling the message selector
•Sending the message to a class or instance that does not support it
•Passing the wrong number of arguments
•Omitting a period or semicolon on the previous statement
•Omitting or mismatching parentheses
•Sending the message to an object that is nil
Missing separators
Missing periods can cause errors that are hard to track down. For example, see what happens when you Execute code that is missing a period:
| aString aChar |
aString := 'abc'.
"The next line is missing a period."
aChar := aString first
aChar = $a ifTrue: Transcript show: 'a'].
This example causes the error: Character does not understand aChar.
Sending the wrong message
A more subtle error occurs when you incorrectly type the message, but the selector you type is valid and is understood by the object. For example, run the following in the System Transcript:
| aCollection |
aCollection := OrderedCollection new.
aCollection add: 2.
aCollection add: 3.
"The next line will not work!"
aCollection add: 1 before: 2.
aCollection inspect.
If you intended to use add:beforeIndex: instead of add:before:, you would not get an error because OrderedCollection objects understand both messages. However, the results would have incorrect values.
Brackets and parentheses
Incorrectly nested brackets and missing parentheses are common errors. Remember that messages are evaluated in this order: unary, binary, and then keyword messages. Arithmetic operators have no special precedence rules, so be sure to use parentheses when necessary.
Run these examples using Display:
7 + 3 * 10. " answers 100, not 37 "
"The next line tries to add 7 and the string '3' "
Transcript show: 7 + 3 printString.
For more information on debugging code, refer to the Smalltalk User Guide.
Last modified date: 07/23/2020