[Tutor] Unhandled exception

Alan Gauld alan.gauld at btinternet.com
Mon Jul 21 11:38:12 CEST 2014


On 21/07/14 07:57, yarteydegreat2 at gmail.com wrote:
> I just want to know if an unhandled exception is a bug in my programs!

No, its not. Maybe.

Exceptions can happen for all sorts of reasons, for
some you can do something if they happen.
In those cases handling the exception makes sense.

In other cases the exception can't be usefully handled
so there is little point in creating a handler. Just
allow the exception to happen and display the stack
trace.

So if you can reasonably expect an exception to happen
and can do something sensible about it (like trying
to open a non existent file, say) you should handle
it and not doing so would arguably be a bug.

But if you can't do anything sensible with the
exception (an out of memory error for example(*))
you might as well just let it happen.

The final twist is that your client might not like
to see user-hostile error messages. I that case
use a general except clause and log the error
where you can find it but the user gets a
nice friendly message instead.

(*)Of course in critical apps running out of
memory could be seen as a bug. But nonetheless
not catching the exception when it happens is
not the problem. Memory errors usually need to be
prevented not handled after the fact.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list