|
3-28-02
Errors and Exceptions by
Erick Tejkowski
Today's tutorial doesn't contain any flashing lights, it won't make your Mac run faster, nor will it make you rich and famous.
But, it will make your software work better and reduce crashes. We will discuss how to make use of REALbasic's exception handlers to
improve the overall integrity of your code. Your users will thank you!
Introduction
Computers are marvelous tools. They can do all sorts of complicated computations very rapidly, giving you the ability to
perform many kinds of tasks with different kinds of media: audio, video, graphics, text processing, databases, video games, 3D, and the list goes on.
The problem with all this power is that its controlled by humans and as well all know, humans make mistakes. The speediest
and sleekest of machines can come crashing down with just one bad line of code.
Errors
Source code errors come in a variety of forms. Some errors are grammatical in nature and cause your program to not run at all. These
are called syntax errors and are caught by REALbasic whenever you attempt to Debug->Run your project.
Some errors result
in incorrect functionality. The program runs and may even work well, but the error has caused something to not work quite right.
They make your program misbehave, but they don't have any serious side effects (e.g. crashing).
Why is the square blue instead of green? Why has the final character in my filename been removed? How come I only see 50 invitations, there should be 100!
These are examples of this type of error.
Other more sinister errors are not so forgiving. They can jump out at you unexpectedly and force your application to come to a
screeching halt. If you are still programming for the Classic Mac OS, these types of errors can even cause your entire machine
to become so unstable as to require a reboot. These errors are to be avoided at all costs! The name of this type of error is exception.
When your code encounters one of these errors, the computer is said have "thrown an exception". If you see this jargon, it just means
that an error has occurred.
Here's a quick rundown of the various exceptions REALbasic might "throw" your way.
- StackOverflowException
- IllegalCastException
- OutOfBoundsException
- TypeMismatchException
- NilObjectException
What to do?
So, what's a REALbasic user to do about these exceptions when they occur? Follow these three steps to make your code less prone to crashes:
1. Add error checking! - The best medicine in bug elimination is not to produce any bugs in the first place.
For example, if you are going to use an object, make sure it exists before you try to use it:
dim f as folderitem
f=SelectFolder
if f<>nil then
MsgBox "We have a folderitem"
else
MsgBox "We don't have a folderitem"
end if
In this example, if we hadn't checked for a nil folderitem, this code would cause an exception when the user clicks the "Cancel" button
during the "select a folder" process.
2. Trap your Exceptions! - Catch those errors when they do occur. If you don't your computer doesn't know what to do with them
and that's when things turn nasty. Open the Code Editor to a method or event and scroll down to the very bottom of that method's code.
Add the following code:
//ERROR HANDLING
Exception err As StackOverflowException
LogError "StackOverflowException "
Exception err As IllegalCastException
LogError "IllegalCastException"
Exception err As OutOfBoundsException
LogError "OutOfBoundsException"
Exception err As TypeMismatchException
LogError "TypeMismatchException"
Exception err As NilObjectException
LogError "NilObjectException"
As your code encounters exceptions, it will jump down to this code segment and perform whatever code is assigned to this exception. In this case
we are sending a string to a "LogError" function. You could choose to do something else. For example, in the SelectFolder code from earlier, you might
catch the nil folderitem in the NilObjectException handler and force the user to select another folder. It's up to you how you want to handle the error, but at least REALbasic
gives you a way to do so. Another beneficial side effect of error exceptions is that they are helpful during testing. For example, you might log each of these
errors to a text file so you can witness where everything went wrong. This is also handy for beta testers to send you feedback, which leads to our next step.
3. Test, test, and test some more!
Again, prevention is key when it comes to bugs. When your program starts to take on any complexity at all, the chances of missing a bug in your code
grow exponentially. No one is perfect and bugs do slip through. To avoid the potential bad press, hurt feelings, and crashing computers, make sure to
test your code over and over. The best way is to get a few friends to help out. Let them run it on their machine without help from you. This way
you can get some real feedback about how someone uses your application. It never fails, you test your application dozens of times and think it works
flawlessly, then you give it to your friend who can break it in seconds. Users can and will do things that you don't expect. They will also
try to use your software on machines you hadn't considered. The best tool for this kind of debugging is testing.
Conclusion
Well, that's it for today. No flashy project, no gimmicky fun, just boring old error talk. But, if you've been paying attention, your
code will work better, break less, and make your users happy. See you next week!
3-26-02
REALbasic News
by
Erick Tejkowski
Cubie's Contest!
REAL Software holds an annual REALbasic contest, called the "Cubies".
The contest to recognize the contributions REALbasic users have made to the
industry by recognizing the excellence of what they have produced
with REALbasic. The contest opens now, and closes April 26, 2002.
REAL Software will announce the finalists the following week and the
winners in each category during Apple's World Wide Developers
Conference.
The contest has the following categories:
- Advocate of the Year
- Cross-Platform
- Developer Tool
- Game
- Internet
- Mac OS X
- Utility
- Overall
- Business
REAL Software will accept nominations from anyone, but no REAL Software employee
or member of their family will be eligible. The only other
requirement is that the products be Premier partners in the
Made with
REALbasic program.
Winners will receive TWO free upgrades to REALbasic, support in
publishing the product (if desired), free publicity, the admiration
and envy of their peers, and other prizes to be announced.
The entries will be judged on quality, fitness to the task (fun game?
useful utility?), polish, verve, and brio. The categories are as
follows:
Mac OS X: The best application for Mac OS X. It need not fall into
any particular category, and can also support the classic Mac OS (and
heck, Windows too).
Business: The best tool for conducting some sort of commercial
activity, whether it is a traditional business application or a tool
for creating stuff that makes money for the user.
Game: The best game. We will know it when we see it, can be pure
entertainment, action, strategy, educational, you name it, it's all
good.
Internet: Best tool for the Internet. This could be anything useful
in that rather large arena, from a server tool, to a decoder, player,
testing tool. If it uses IP (or its cousins) or some known protocol
or format, it's in!
Cross-platform: The best example of an application that embraces both
the Mac OS and Windows.
Utility: The best application that does something useful. This too is
intentionally broad! All it has to do is crank out the utilities, and we
will be all over it.
REALbasic development aid: This includes plug-ins, classes, modules,
frameworks, pre- and post-processors, anything that helps some
REALbasic developer get the job done.
Overall: The very best REALbasic application we receive. It can be a
double winner, or it can be one of those None of the Above fliers
that just blows us away.
Advocate of the Year: Finally, there's a special category for the
REALbasic advocate of the year, that person who has done the most for
the community, and helped make REALbasic known everywhere as the
truly great tool that it is.
These are all open for nominations from the community. To nominate
someone, or some product, send email to
nominations@realsoftware.com. Don't be shy, either! Nominate
yourself! It's not a vote, though, so no need to stuff the ballot box!
Check out last year's winners here.
REAL Software's Many Releases
The folks at REAL Software have been hard at work in their new office space to bring you many fine new downloads:
REALbasic Developer Magazine - Subscribe Online Now!
REALbasic Developer, the
exciting new print magazine devoted to REALbasic programming launching
this summer, is now accepting subscriptions! Best of all, if you're one of the first 500 subscribers before April 15,
2002, you will qualify as a Charter Member and receive your subscription
at half off the standard price!
You may subscribe online via secure server, or by mail via check or money
order.
Control Focus plugin
The Smith has posted a new plugin: Control Focus plugin v1.
This plugin gives all Controls, the property "HasFocus as boolean",
which does exactly that.
|