[Tutor] help with a recursive function

Steven D'Aprano steve at pearwood.info
Wed Sep 28 01:58:49 CEST 2011


Mac Ryan wrote:

>     raise BaseException('Something is wrong here!')

Never raise BaseException directly! BaseException is the very top of the 
exception hierarchy, you should raise the *most* specific exception you 
can, not the least specific. BaseException isn't even just for errors, 
it's also for flow control exceptions like StopIteration .

At worst, you should raise StandardError, but even that is a pretty 
dodgy thing to do. In general you want something more specific, like 
ValueError or TypeError. In this particular case, the appropriate error 
would be to raise RuntimeError: if you're ever in the position of having 
to just give up and say "I have no idea what is wrong, but something is 
wrong", then RuntimeError is the one to use.


More about exceptions here:

http://docs.python.org/library/exceptions.html


-- 
Steven


More information about the Tutor mailing list