在一段程序中,为了能够让程序健壮,必须要处理异常。举例:
#!/usr/bin/env python # coding=utf-8 while 1: print "this is a division program." c = raw_input("input 'c' continue, otherwise logout:") if c == 'c': a = raw_input("first number:") b = raw_input("second number:") try: print float(a)/float(b) print "*************************" except ZeroDivisionError: print "The second number can't be zero!" print "*************************" else: break -----------------------------------
处理异常的方式之一,使用
try...except...
。对于上述程序,只看try和except部分,如果没有异常发生,except子句在try语句执行之后被忽略;如果try子句中有异常可,该部分的其它语句被忽略,直接跳到except部分,执行其后面指定的异常类型及其子句。
except后面也可以没有任何异常类型,即无异常参数。如果这样,不论try部分发生什么异常,都会执行except。