Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > execution error

Reply
Thread Tools

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.


 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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



 
Reply With Quote
 
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:
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.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
Club Site Starter Kit and Error during the execution of the SQL fi =?Utf-8?B?bmo=?= ASP .Net 0 06-09-2006 08:38 PM
Parser Error Message: Execution permission cannot be acquired. Goffin ASP .Net 0 02-16-2006 01:04 PM
Fatal Execution Engine Error (0 x 7924b50b) Alan Z. Scharf ASP .Net 3 01-25-2005 10:47 PM
Re: Parser Error Message: Execution permission cannot be acquired. Chris Boyd ASP .Net 0 10-15-2003 04:41 AM



Advertisments