[Tutor] when to use exceptions
alan.gauld@bt.com
alan.gauld@bt.com
Fri, 19 Jul 2002 13:22:51 +0100
> Where do you draw the line of when to use an exception and
> when not to use an exception?
By use I assume you mean writing a handler.
The answer then becomes "when you can do something useful"
If you can't do amnything useful then write a generic
handler that prints out a friendly error message, logs the
useful info and then exits.
In practice I usually find that means writing 2 or 3 at
most specific handlers and one generic handler.
Often, if the try block is short then its only a single
handler for a specific error type that is most likely
to occur- Divide by zero, IO error etc.
> exceptions for every conceiveable problem... but you would
> spend all of your time writing exception-catching code, right?
<Puts large scale programming hat on again!>
In commercial C++ programs its not uncommon to find
that 30-50% of the code consists of error handlers.
Exception handling helps reduce that but old style C
error handling(still necedssary in many C++ libraries)
led to very verbose error handling code.
In my experience of Python(ie no large scale projects)
the percentage of error handling code is much reduced but
if you really wanted a bulletproof system then yes,
you could write as much error handler code as core
application.
Alan g.