execution error

X

Ximo

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

Thank you.
 
P

Paul Rubin

Ximo said:
And my question is how can show the execution error whitout exit of the
program, showing it in the error output as...

Trap the ArithmeticError exception and go by the name of the exception
class. See the language ref manual about how exceptions work.
 
S

Skip Montanaro

Ximo> And my question is how can show the execution error whitout exit
Ximo> of the program, showing it in the error output as

You need to catch ZeroDivisionError. Here's a trivial example:
... 6/0
... except ZeroDivisionError:
... print "whoops! divide by zero..."
...
whoops! divide by zero...

Skip
 
P

Paul McNett

Ximo said:
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.
 
X

Ximo

Skip Montanaro said:
Ximo> And my question is how can show the execution error whitout exit
Ximo> of the program, showing it in the error output as

You need to catch ZeroDivisionError. Here's a trivial example:

... 6/0
... except ZeroDivisionError:
... print "whoops! divide by zero..."
...
whoops! divide by zero...

But I execute the operation in the else:

try:
except ZeroDivisionError:
print "whoops! divide by zero..."
else:
execute( )

The exeption is produced after.
 
D

Dennis Lee Bieber

But I execute the operation in the else:

try:
except ZeroDivisionError:
print "whoops! divide by zero..."
else:
execute( )

The exeption is produced after.

Well... the above is not even valid Python syntax.

The "except...:" has to align with the "try:", and the
statements that may generate the exception come between the "try:" and
the "except...:".

try:
execute()
except ...:
print "error"

An "else:" clause on a "try:" is executed only if no exceptions
occur IN the block. From the language reference (you have read this,
haven't you?"

PLR> The optional else clause is executed if and when control flows off
the end of the try clause.7.1 Exceptions in the else clause are not
handled by the preceding except clauses.

(the 7.1 is a footnote marker)


In a very contrived example, and without specifying particular
exceptions:

try:
fi = open("some.file")
try:
while 1:
dta = fi.readline()
if not dta: break
#do stuff
except ...:
print "Error Reading"
except ...:
print "Error opening"
else:
fi.close()

This example presumes that a failure opening the file means there is
nothing to close. Otherwise it processes the file contents. An error
reading the file will not raise an exception in the outer try, so the
outer try exits via the else clause -- which closes the open file; if
the file wasn't opened the outer except triggered, and the else is not
used.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top