Catching an unknown error

Discussion in 'Python' started by Harlin Seritt, Mar 23, 2007.

  1. Using the code below:

    ---BEGIN CODE---

    value = raw_input("Type a divisor: ")
    try:
    value = int(value)
    print "42 / %d = %d" % (value, 42/value)
    except ValueError:
    print "I can't convert the value to an integer"
    except ZeroDivisionError:
    print "Your value should not be zero"
    except:
    print "Something unexpected happened"

    ---END CODE---

    In the last 'except' block, how can I print out the particular error
    name even though one is not specifically named?

    Thanks,

    Harlin
    Harlin Seritt, Mar 23, 2007
    #1
    1. Advertising

  2. Harlin Seritt

    Guest

    On Mar 23, 8:16 am, "Harlin Seritt" <> wrote:
    > Using the code below:
    >
    > ---BEGIN CODE---
    >
    > value = raw_input("Type a divisor: ")
    > try:
    > value = int(value)
    > print "42 / %d = %d" % (value, 42/value)
    > except ValueError:
    > print "I can't convert the value to an integer"
    > except ZeroDivisionError:
    > print "Your value should not be zero"
    > except:
    > print "Something unexpected happened"
    >
    > ---END CODE---
    >
    > In the last 'except' block, how can I print out the particular error
    > name even though one is not specifically named?
    >
    > Thanks,
    >
    > Harlin


    Make the last 'except' block like this:

    Except Exception, e:
    print e


    Mike
    , Mar 23, 2007
    #2
    1. Advertising

  3. Harlin Seritt wrote:

    > In the last 'except' block, how can I print out the particular error
    > name even though one is not specifically named?


    the sys.exc_info() function returns information about the current exception.
    see:

    http://effbot.org/pyref/sys.exc_info

    </F>
    Fredrik Lundh, Mar 23, 2007
    #3
  4. Harlin Seritt

    Guest

    Harlin> value = raw_input("Type a divisor: ")
    Harlin> try:
    Harlin> value = int(value)
    Harlin> print "42 / %d = %d" % (value, 42/value)
    Harlin> except ValueError:
    Harlin> print "I can't convert the value to an integer"
    Harlin> except ZeroDivisionError:
    Harlin> print "Your value should not be zero"
    Harlin> except:
    Harlin> print "Something unexpected happened"

    Harlin> In the last 'except' block, how can I print out the particular
    Harlin> error name even though one is not specifically named?

    >>> try:

    .... 1/0
    .... except Exception, err:
    .... print repr(err)
    ....
    <exceptions.ZeroDivisionError instance at 0x81e96ac>

    Skip
    , Mar 23, 2007
    #4
  5. Harlin Seritt

    Guest

    On Mar 23, 8:29 am, wrote:
    > On Mar 23, 8:16 am, "Harlin Seritt" <> wrote:
    >
    >
    >
    > > Using the code below:

    >
    > > ---BEGIN CODE---

    >
    > > value = raw_input("Type a divisor: ")
    > > try:
    > > value = int(value)
    > > print "42 / %d = %d" % (value, 42/value)
    > > except ValueError:
    > > print "I can't convert the value to an integer"
    > > except ZeroDivisionError:
    > > print "Your value should not be zero"
    > > except:
    > > print "Something unexpected happened"

    >
    > > ---END CODE---

    >
    > > In the last 'except' block, how can I print out the particular error
    > > name even though one is not specifically named?

    >
    > > Thanks,

    >
    > > Harlin

    >
    > Make the last 'except' block like this:
    >
    > Except Exception, e:
    > print e
    >
    > Mike


    just don't capitalize the word "except" ... my bad
    , Mar 23, 2007
    #5
  6. wrote:

    > Make the last 'except' block like this:
    >
    > Except Exception, e:
    > print e


    while that's good enough for the given example, it's not good enough for
    the general case (in contemporary Python, exceptions don't have to inherit
    from the Exception class).

    </F>
    Fredrik Lundh, Mar 23, 2007
    #6
  7. Harlin Seritt

    Guest

    On Mar 23, 8:16 am, "Harlin Seritt" <> wrote:
    > Using the code below:
    >
    > ---BEGIN CODE---
    >
    > value = raw_input("Type a divisor: ")
    > try:
    > value = int(value)
    > print "42 / %d = %d" % (value, 42/value)
    > except ValueError:
    > print "I can't convert the value to an integer"
    > except ZeroDivisionError:
    > print "Your value should not be zero"
    > except:
    > print "Something unexpected happened"
    >
    > ---END CODE---
    >
    > In the last 'except' block, how can I print out the particular error
    > name even though one is not specifically named?
    >
    > Thanks,
    >
    > Harlin


    Thanks for pointing that out. I was following logic I was taught in
    Hetland's book, which supposedly was up-to-date for Python 2.4.
    Typical textbook error.

    Mike
    , Mar 23, 2007
    #7
  8. On Mar 23, 9:42 am, wrote:
    > On Mar 23, 8:16 am, "Harlin Seritt" <> wrote:
    >
    >
    >
    > > Using the code below:

    >
    > > ---BEGIN CODE---

    >
    > > value = raw_input("Type a divisor: ")
    > > try:
    > > value = int(value)
    > > print "42 / %d = %d" % (value, 42/value)
    > > except ValueError:
    > > print "I can't convert the value to an integer"
    > > except ZeroDivisionError:
    > > print "Your value should not be zero"
    > > except:
    > > print "Something unexpected happened"

    >
    > > ---END CODE---

    >
    > > In the last 'except' block, how can I print out the particular error
    > > name even though one is not specifically named?

    >
    > > Thanks,

    >
    > > Harlin

    >
    > Thanks for pointing that out. I was following logic I was taught in
    > Hetland's book, which supposedly was up-to-date for Python 2.4.
    > Typical textbook error.
    >
    > Mike


    Thanks guys... that gets 'er done.

    Harlin Seritt
    Harlin Seritt, Mar 23, 2007
    #8
    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. PapaBear
    Replies:
    2
    Views:
    1,210
  2. =?Utf-8?B?bXVsdGlzeW5j?=
    Replies:
    0
    Views:
    2,149
    =?Utf-8?B?bXVsdGlzeW5j?=
    Dec 13, 2005
  3. Bryan
    Replies:
    3
    Views:
    328
  4. Mark Devine
    Replies:
    2
    Views:
    1,061
    amadain
    Jun 29, 2009
  5. Vincent Arnoux
    Replies:
    1
    Views:
    226
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page