execution error

Discussion in 'Python' started by Ximo, May 23, 2005.

  1. Ximo

    Ximo Guest

    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.
    Ximo, May 23, 2005
    #1
    1. Advertising

  2. Ximo

    Paul Rubin Guest

    "Ximo" <> 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.
    Paul Rubin, May 23, 2005
    #2
    1. Advertising

  3. 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
    Skip Montanaro, May 23, 2005
    #3
  4. Ximo

    Paul McNett Guest

    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
    Paul McNett, May 23, 2005
    #4
  5. Ximo

    Ximo Guest

    "Skip Montanaro" <> escribió en el mensaje
    news:...
    >
    > 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
    Ximo, May 24, 2005
    #5
  6. On Tue, 24 May 2005 16:39:33 +0200, "Ximo" <> 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.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, May 24, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Boyd
    Replies:
    0
    Views:
    412
    Chris Boyd
    Oct 15, 2003
  2. Alan Z. Scharf

    Fatal Execution Engine Error (0 x 7924b50b)

    Alan Z. Scharf, May 12, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,411
    Sankar
    Jan 25, 2005
  3. Goffin
    Replies:
    0
    Views:
    632
    Goffin
    Feb 16, 2006
  4. =?Utf-8?B?bmo=?=
    Replies:
    0
    Views:
    639
    =?Utf-8?B?bmo=?=
    Jun 9, 2006
  5. omission9
    Replies:
    3
    Views:
    341
    Ziaran
    Feb 23, 2004
Loading...

Share This Page