Catching an unknown error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlin Seritt

    Catching an unknown error

    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 ZeroDivisionErr or:
    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

  • kyosohma@gmail.com

    #2
    Re: Catching an unknown error

    On Mar 23, 8:16 am, "Harlin Seritt" <harlinser...@y ahoo.comwrote:
    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 ZeroDivisionErr or:
    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

    Comment

    • kyosohma@gmail.com

      #3
      Re: Catching an unknown error

      On Mar 23, 8:29 am, kyoso...@gmail. com wrote:
      On Mar 23, 8:16 am, "Harlin Seritt" <harlinser...@y ahoo.comwrote:
      >
      >
      >
      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 ZeroDivisionErr or:
      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

      Comment

      • Fredrik Lundh

        #4
        Re: Catching an unknown error

        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:



        </F>



        Comment

        • Fredrik Lundh

          #5
          Re: Catching an unknown error

          kyosohma@gmail. com 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>



          Comment

          • kyosohma@gmail.com

            #6
            Re: Catching an unknown error

            On Mar 23, 8:16 am, "Harlin Seritt" <harlinser...@y ahoo.comwrote:
            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 ZeroDivisionErr or:
            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

            Comment

            • Harlin Seritt

              #7
              Re: Catching an unknown error

              On Mar 23, 9:42 am, kyoso...@gmail. com wrote:
              On Mar 23, 8:16 am, "Harlin Seritt" <harlinser...@y ahoo.comwrote:
              >
              >
              >
              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 ZeroDivisionErr or:
              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

              Comment

              • skip@pobox.com

                #8
                Re: Catching an unknown error


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

                HarlinIn the last 'except' block, how can I print out the particular
                Harlinerror name even though one is not specifically named?
                >>try:
                .... 1/0
                .... except Exception, err:
                .... print repr(err)
                ....
                <exceptions.Zer oDivisionError instance at 0x81e96ac>

                Skip

                Comment

                Working...