try-except Statement : Error Exception : Statement PYTHON TUTORIALS


PYTHON TUTORIALS » Statement » Error Exception »

 

try-except Statement


try:
    f = open('blah', 'r')
except IOError, e:
    print 'could not open file:', e
    
print float(12345)
print float('12345')
print float('123.45e67')

def safe_float(obj):
     try:
         retval = float(obj)
     except ValueError:
          retval = 'could not convert non-number to float'
     return retval

print safe_float('12.34')
print safe_float('bad input')
print safe_float({'a''Dict'})



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Statement
» Error Exception