[Tutor] Error handling in functions / main program

Kent Johnson kent_johnson at skillsoft.com
Thu Aug 12 17:35:05 CEST 2004


In general I think it is a good idea if functions check there inputs for 
validity and generate a meaningful error. That way, if the function is used 
by multiple clients, you don't have to duplicate the checking code.

A lot depends on who will be using the program and what the cost is of an 
error. If the program is just for you and it is easy to set up and fast to 
run, it might be OK to leave out any error checking and have it terminate 
with a stack trace when there is an error. You will understand what the 
stack trace is telling you, kick yourself for your silly mistake, and try 

On the other hand if the program is for use by other people, or if it 
requires a complex setup or takes a long time to run, you might want to be 
more careful with your error checking and making helpful error messages. 
You don't want a program to run for 5 minutes and then crash because you 
misspelled an argument, and you don't want users to have to come to you and 
ask you 'what does this mean' because they got a stack trace.

For me a lot of error checking is reactive, because you don't know in 
advance what kinds of errors users will get. Of course you try to think it 
through in advance, but you will always be surprised. Many errors will be 
due to unexpected inputs or unexpected environment. When a user comes to 
you with a problem, ask yourself, "How could I have detected that? What 
could the program do so the user can figure out the problem himself?" Make 
the change so you don't get that question any more.


At 05:09 PM 8/12/2004 +0200, Klas Marteleur wrote:
>Now to my questions:
>How much and what type of error handling should be handled by:
>1. The functions
>2 The main program / GUI program.
>I mean i could add a lot of extra error checking in the functions (... so 
>correct inputs are used etc)? Or should that be the responsebility of the
>main program to asure that the input to the functions are correct?
>Then what type of error handling should be located in the functions?

More information about the Tutor mailing list