python异常信息捕获方法学习
Python的异常处理能力是很强大的,在Python开发中,异常也是对象,可对它进行操作。下面就简单介绍下自定义异常类和捕捉异常的方法。1/0就会产生异常。按自己的方式出错raise语句>>>raise ExceptionTraceback (most recent all last):自定义异常类class SomeCustomException(Exception):pass捕捉异常 try/excepttry:x = input(’Enter the first number: ’)y = input(’Enter the second number: ’)print x/yexcept ZeroDivisonError:print ’The second number can’t be zero!’多个excepttry:x = input(’Enter the first number: ’)y = input(’Enter the second number: ’)print x/yexcept ZeroDivisonError:print ’The second number can’t be zero!’except TypeError:print ’That wasn’t a number, was it?’一个excep捕捉多个异常try:x = input(’Enter the first number: ’)y = input(’Enter the second number: ’)print x/yexcept (ZeroDivisonError, TypeError, NameError):print ’Your numbers were bogus...’try/except/elsetry:print’ ’except:print’ ’else:print’ ’finally语句不管是否引发了异常,finall语句都会执行。
页:
[1]