Velocity Reviews > execution error

# execution error

Ximo
Guest
Posts: n/a

 05-23-2005
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

>>2+2
>>4
>>3*4
>>12
>>6/0
>>"Error: Division per 0"

Thank you.

Paul Rubin
Guest
Posts: n/a

 05-23-2005
"Ximo" <(E-Mail Removed)> writes:
> And my question is how can show the execution error whitout exit of the
> program, showing it in the error output as...
> >>6/0
> >>"Error: Division per 0"

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

Skip Montanaro
Guest
Posts: n/a

 05-23-2005

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:

>>> try:

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

Skip

Paul McNett
Guest
Posts: n/a

 05-23-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

Ximo
Guest
Posts: n/a

 05-24-2005

"Skip Montanaro" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
>
> 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:
>
> >>> try:

> ... 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.

>
> Skip

Dennis Lee Bieber
Guest
Posts: n/a

 05-24-2005
On Tue, 24 May 2005 16:39:33 +0200, "Ximo" <(E-Mail Removed)> declaimed
the following in comp.lang.python:

> 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:
if not dta: break
#do stuff
except ...:
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.

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <