It's said that between a half and 95% of a program will be given over to error checking. Indeed some of the web based programs on this site are little more than a single database lookup. But they wrapped in a whole lot of extra code that ensures that the entries are sensible and valid before they're used. These days we have to check for malicious attempts to break in too.
There are two approaches to programming to check the user's input. You'll almost always want some form or error checking, but if you're using a language that supports exceptions, you'll probably want to use those too.
ERROR CHECKING
With error checking, you test the data you have to see if a certain condition holds, and if it does you take appropriate action based on conditional statements.
For example, if you've asked a user to supply his email address, a good test would be to see if it contains an @ character; if it fails to do so, handle it as an error.
With error checking, you'll also want to check the return status of function calls you make to ensure that they're telling you that they were successful - for example in opening a file. But in languages that have exception handlers, a try and catch block may be a better alternative.
CATCHING EXCEPTIONS
With exceptions, you don't check for certain errors ahead of time - you go ahead and write your function / method calls as if you knew they were going to return you a good response ...
BUT ....
You wrap your code that may fail into a try block. What you're saying is "please try out this code and let me know if you fail".
Should the code in your try block not run successfully, an exception will be thrown. Your code execution will be diverted to a catch or except block of code (it depends on the language) which you provide in order to handle the condition.
WHEN TO USE EXCEPTIONS
1. When it would need a lot of prior testing for error conditions
2. For things you can't easily test - for example whether or not you can establish a connection to a remote machine in a networked application
3. Where you want a neat mechanism for recovery
EXCEPTIONS ARE MORE THAN JUST FANCY ERROR HANDLERS
It turns out that exceptions go much further than error handling and they're a truly useful mechanism for returning conditions that don't meet the normal criteria.
For example, if you write a function that returns a list of the occupants of a house, you might well have it throw exceptions if you don't know who lives in a house, or if you don't know details of the house at all.
FOOTNOTE
You know how important it is to get the scope of your if and while blocks correct? Yes - of course you do - that's fundamental programming. It is equally important to get the scope of your try and catch blocks right - to ensure that any conditions that are caught are handled at the right level. Please do NOT just put a try block around your whole program ;-)
See also
Articles comparing languages
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Language Comparisons [209] - ()
[1582] - ()
[1717] - ()
[1990] - ()
[2700] - ()
[2755] - ()
[2866] - ()
[2947] - ()
[3003] - ()
[3112] - ()
[3169] - ()
[3785] - ()
Java - Exceptions [1066] - ()
[1875] - ()
[2420] - ()
[2622] - ()
[2862] - ()
[3045] - ()
[3048] - ()
[4350] - ()
[4396] - ()
Ruby - Exceptions. [1875] - ()
[2615] - ()
[2620] - ()
[2621] - ()
[2622] - ()
[3177] - ()
[3260] - ()
[3433] - ()
[3435] - ()
[4008] - ()
[4675] - ()
Tcl/Tk - Other Facilities in Tcl [239] - ()
[364] - ()
[366] - ()
[461] - ()
[748] - ()
[782] - ()
[1277] - ()
[1334] - ()
[1338] - ()
[2467] - ()
[3287] - ()
[3570] - ()
[3583] - ()
[4207] - ()
[4523] - ()
[4525] - ()
[4762] - ()
C and C based languages - Exceptions [799] - ()
[1875] - ()
[2622] - ()
[3068] - ()
[3509] - ()
Python - Exceptions [381] - ()
[1042] - ()
[1236] - ()
[2018] - ()
[2281] - ()
[2368] - ()
[2408] - ()
[2622] - ()
[2994] - ()
[2998] - ()
[3177] - ()
[3433] - ()
[3441] - ()
[3664] - ()
[3913] - ()
[3930] - ()
[4029] - ()
[4161] - ()
[4444] - ()
resource index - Deployment
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.