exceptions == errors?

Roy Smith roy at panix.com
Mon Apr 7 20:20:13 EDT 2003


Mark Harrison <mh at pixar.com> wrote:
> Are exceptions always considered errors, or there the same feeling
> as in C++ that exceptions can be used for non-error situations?

I think of exceptions as being anything out of the normal flow of 
processing.

I'm more fluent in Python than I am in C++, so I may be wrong here, but 
my perception is that the C++ community treats exceptions with a lot 
more fear and lothing than the Python world does.  I suspect part of the 
problem is that they are relatively more expensive in C++.  They are 
also relatively non-portable, and somewhat complex to fully understand 
how they interact with threads and object creation/destruction.

In Python, exceptions seem much more natural and lightweight.  I don't 
see any reason not to use exceptions liberally if they make the control 
flow easier to understand
 
> 	while 1:
> 		try:
> 			read request
> 			process request
> 		catch MyException,e:
> 			send response contained in e
> 		catch Exception,e:
> 			send "internal error" response

That seems perfectly plausable.  I assume that your "process request" 
line includes sending some normal response to the client.  You've now 
got all three possible flow paths covered -- normal operation, abnormal 
operation handled by the server, and "should never happen" faults which 
at least you handle gracefully (better to tell the client that you've 
lost your mind than to just stop talking).

And, of course, "catch" is spelled "except" in Python :-)




More information about the Python-list mailing list