Catching an unknown error

H

Harlin Seritt

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
 
K

kyosohma

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
 
S

skip

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?
.... 1/0
.... except Exception, err:
.... print repr(err)
....
<exceptions.ZeroDivisionError instance at 0x81e96ac>

Skip
 
F

Fredrik Lundh

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

kyosohma

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
 
H

Harlin Seritt

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top