![]() 3D 3D Photo Gallery (Part 1) 3D Photo Gallery (Part 2) Audio Poor Man's MIDI Make A Metronome iPod Tricks (Part 1) iPod Tricks (Part 2) iPod Tricks (Part 3) Laugh Track Machine Audio Player with Reverb Shepard Melody RB Phone Home Build a Drum Machine Custom Controls and Windows Double Click Listbox Draggable Metal Window Double Click Canvas Custom Buttons Custom Buttons Part II iTunes-style Listboxes Custom Controls General RB Scrolling Windows Using Mesage Dialogs Case-Sensitive Word Finder Introduction to Stacks Wiggle Window JPEG in PDF Listbox Checkboxes Background Applications Listbox Auto-Find Virtual Volumes Time Tracker Software Distribution (Part 1) Software Distribution (Part 2) Software Distribution (Part 3) Software Distribution (Part 4) Exceptions Tips and Tricks Text Clippings Made Easy Graphics Drawing a Simple Gradient The SpriteSurface: Space Game Image Spinner Cropping Graphics (Part 1) Cropping Graphics (Part 2) Cropping Graphics (Part 3) Cropping Graphics (Part 4) Shimmer Graphics Lissajous Figures Simple Screen Capture Vector Graphics Kaleidoscope Images Stegonography Spirals! Image Table RB Magnifying Lens Screen Capture Color Picker Tutorial Hacks Ghost Grab Speedy Mouse Extension iTunes Plugins iTunes Skinner Mac OS X Global Hot Key Event (Carbon Events) Login Welcomer (Carbon Events) Add/Remove Buttons Resizable Sheets Mac OS X Preferences Window Using Sheets in REALbasic Build a Bundle (Part 1) Build a Bundle (Part 2) Dock Your Passwords Mac OS X Debugging REALbasic Mac OS X Icon Tutorial Animate Your Dock RB and the Command Line Menus Window Menu Templates Menu Listbox Menu Novelty Guessing Game Calendar Trivia Tile Mixer Zip Code Finder Happy Valentine's Day Merlin Simulator (Part 1) Merlin Simulator (Part 2) Merlin Simulator (Part 3) Buzzword Machine AppleSoft BASIC Printing Print to PDF Registration Registration Code Validation Network Registration Codes Resources Picture Extractor (Part 1) Picture Extractor (Part 2) Serial Caller ID (Part 1) Caller ID (Part 2) Caller ID (Part 3) Speech Speech Recognition Socket Communication Easy Peer-to-Peer File Sharing MacPAD Version Checking Display Web Image In Canvas HTML IMG Tags Version Tracking Even Smarter Instant Messaging Web Tiler JavaScript and REALbasic Stock Ticker (Part I) Stock Ticker (Part 2) AIM Mate XML Manipulation Simple XML Introduction Video Big Brother Video Capture Note: All articles without a byline were written by Erick Tejkowski. When cleaning the site I removed them because the code differed from page to page, and I have yet to put them back in.
Tell us about a bad link. |
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.
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 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 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! |
|||||
|
Please support ResExcellence by Visiting our Sponsors. One click makes a difference. |
||||||
|
|