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
Language Comparisons [3169] Rekeying a table - comparison in #Ruby #Perl and #Python - (2011-02-14)
[3112] Public and private courses - subjects available for 2011 - (2010-12-29)
[3003] What will we be teaching in six years? - (2010-10-17)
[2947] Teaching Lua to a Perl advocate - (2010-09-06)
[2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
[2755] Books in the store in the USA - still a portent of the UK market to come? - (2010-05-08)
[2700] The same very simple program in many different programming languages - (2010-03-31)
[1990] Speaking all the languages - (2009-01-12)
[1717] Q - Should I use Perl or Python? - (2008-07-23)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[209] FAQ - Perl or PHP - (2005-02-11)
Java - Exceptions [3048] String handling - from first steps to practical examples - (2010-11-13)
[3045] After Course Resources - do we publish sample answers. Example from Java Exceptions module. - (2010-11-13)
[2862] Fail Safe Error Handling in Java via Exceptions - (2010-07-09)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[2420] Exceptions in Java - why and how - (2009-09-24)
[1875] What are exceptions - Python based answer - (2008-11-08)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
Ruby - Exceptions. [3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2615] String to number conversion with error trapping in Ruby - (2010-02-01)
[1875] What are exceptions - Python based answer - (2008-11-08)
Tcl/Tk - Other Facilities in Tcl [3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
[3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
[3287] Exceptions - Tcl style - (2011-05-12)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[461] Shortened interactive commands - (2005-10-11)
[366] Error handling in Tcl through catch - (2005-07-02)
[364] pu daily and p hourly - (2005-06-30)
[239] What and why for the epoch - (2005-03-08)
C and C based languages - Exceptions [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3068] Throwing your own exception in C++, and catching it - (2010-11-24)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[1875] What are exceptions - Python based answer - (2008-11-08)
[799] The case for exceptions - (2006-07-11)
Python - Exceptions [3441] Pressing ^C in a Python program. Also Progress Bar. - (2011-09-15)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[2408] Robust user input (exception handling) example in Python - (2009-09-17)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2281] Python - using exceptions to set a fallback - (2009-07-12)
[2018] UnboundLocalError - Python Message - (2009-01-31)
[1236] Trying things in Python - (2007-06-18)
[1042] Nested exceptions in Python - (2007-01-18)
[381] Exceptions in Python - (2005-07-17)
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.