29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15. Some More Tools of the Trade<br />

15.1 <strong>Introduction</strong><br />

In this chapter you are introduced to the use of breakpoints <strong><strong>an</strong>d</strong> user initiated exceptions. Breakpoints in<br />

particular are extremely useful <strong><strong>an</strong>d</strong> you will find that you will make extensive use of them, not just for<br />

debugging but also to help you underst<strong><strong>an</strong>d</strong> how objects are behaving. The use of the file in <strong><strong>an</strong>d</strong> file out<br />

facilities for saving classes onto your host file system are also discussed. These are import<strong>an</strong>t as without<br />

this ability you would loose <strong>an</strong>y source code you developed when you started a new image. Finally the<br />

use of the ch<strong>an</strong>ges file <strong><strong>an</strong>d</strong> projects are discussed.<br />

15.2 Errors <strong><strong>an</strong>d</strong> breakpoints<br />

As in most l<strong>an</strong>guages, it is possible to generate user specified errors (sometimes known as mishaps or<br />

exceptions in other l<strong>an</strong>guages). This is achieved by sending the message error to <strong>an</strong> object. Errors are<br />

usually intended to force the user of the system (which may be <strong>an</strong>other developer) to stop executing <strong><strong>an</strong>d</strong><br />

investigate the problem. A similar operation is provided by the halt message. The only real difference<br />

is that the halt message is intended as a debugging aid, while the error message is intended for use in<br />

undesired situations.<br />

15.2.1 Programmer generated errors<br />

Programmer generated errors are really exceptions, which are not h<strong><strong>an</strong>d</strong>led by the system. These<br />

exceptions are generated by the message error. For example:<br />

self error<br />

This message would result in <strong>an</strong> exception dia log being displayed. These dialogs allow the user a<br />

number of options, however, you should notice that the “proceed” option is grayed out. This is because<br />

proceeding is not <strong>an</strong> option if <strong>an</strong> error has been encountered.<br />

If you wish, you c<strong>an</strong> use a version of t he error message which will take a string as a parameter. This<br />

string is displayed to the user <strong><strong>an</strong>d</strong> c<strong>an</strong> act to provide further information about the error. For example:<br />

self error: 'This is not allowed!'.<br />

Note that <strong>an</strong> error message is a message just l ike <strong>an</strong>y other. You c<strong>an</strong> therefore use it <strong>an</strong>ywhere that you<br />

would use a message expression, this me<strong>an</strong>s for example that you c<strong>an</strong> conditionalize the call to error:<br />

(x < 0)<br />

ifTrue: [self error: ‘Divide by zero coming’].<br />

z := y / x.<br />

Have a look at the add: method defined within the Array class of objects for <strong>an</strong> example of <strong>an</strong> error<br />

h<strong><strong>an</strong>d</strong>ling method. You c<strong>an</strong> do this by selecting the “find class” option off the right mouse button (middle<br />

on a three button mouse) in a browser.<br />

15.2.2 Breakpoints<br />

Breakpoints are often useful in determining what is really happening inside the system. In <strong>Smalltalk</strong>,<br />

breakpoints simply provide a controlled mech<strong>an</strong>ism for entering the debugger at a known point in the<br />

execution.<br />

122

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!