execution error

Paul McNett p at ulmcnett.com
Mon May 23 13:40:35 EDT 2005


Ximo wrote:
> Hello, I'm programing an advanced calculator, and I have many problems with 
> the execution errors, specually with the division by 0.
> 
> And my question is how can show the execution error whitout exit of the 
> program, showing it in the error output as


Wrap the math in a try/except block such as:

# Handle the zero-division plus maybe some others:
exceptions_to_handle = (ZeroDivisionError,)

try:
	# do the math here
	result = eval(my_math)
except exceptions_to_handle, e:
	# gracefully handle here
	result = str(e)
# show the result in the calculator here
print result

... or something like that. The point is you need to handle the zero 
division error while not handling other, unexpected error conditions.



-- 
pkm ~ http://paulmcnett.com




More information about the Python-list mailing list