文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>《python 从入门到精通》 §8 例外

《python 从入门到精通》 §8 例外

时间:2009-08-18  来源:oychw

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CNeil%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><style> </style>

§8    例外

§8.1 什么是例外

>>> 1/0

Traceback (most recent call last):

File "<stdin>", line 1, in ?

ZeroDivisionError: integer division or modulo by zero

 

§8.2 出现例外的方法

>>> raise Exception('hyperdrive overload')

Traceback (most recent call last):

File "<stdin>", line 1, in ?

Exception: hyperdrive overload

 

查看例外种类:

>>> import exceptions

>>> dir(exceptions)

['ArithmeticError', 'AssertionError', 'AttributeError', ...]

 

>>> raise ArithmeticError

Traceback (most recent call last):

File "<stdin>", line 1, in ?

ArithmeticError

重要的例外种类见教材186页

 

自定义例外类:

class SomeCustomException(Exception): pass

§8.3 获取例外

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

 

Enter the first number: 10

Enter the second number: 0

Traceback (most recent call last):

File "exceptions.py", line 3, in ?

print x/y

ZeroDivisionError: integer division or modulo by zero

 

try:

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

except ZeroDivisionError:

print "The second number can't be zero!"

 

也可以用if语句来判断,不过如果有多个分母,一个try就可以搞定了。

 

在没有交互式的情况,使用print更好:

class MuffledCalculator:

muffled = False

def calc(self, expr):

try:

return eval(expr)

except ZeroDivisionError:

if self.muffled:

print 'Division by zero is illegal'

else:

raise

 

>>> calculator = MuffledCalculator()

>>> calculator.calc('10/2')

5

>>> calculator.calc('10/0') # No muffling

Traceback (most recent call last):

File "<stdin>", line 1, in ?

File "MuffledCalculator.py", line 6, in calc

return eval(expr)

File "<string>", line 0, in ?

ZeroDivisionError: integer division or modulo by zero

>>> calculator.muffled = True

>>> calculator.calc('10/0')

Division by zero is illegal

 

同时捕捉多个错误:

try:

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

except ZeroDivisionError:

print "The second number can't be zero!"

except TypeError:

print "That wasn't a number, was it?"

 

try:

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

except (ZeroDivisionError, TypeError, NameError):

print 'Your numbers were bogus...'

 

用log记录例外:

try:

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

except (ZeroDivisionError, TypeError), e:

print e

 

捕捉所有错误:

try:

x = input('Enter the first number: ')

y = input('Enter the second number: ')

print x/y

except:

print 'Something wrong happened...'

       一般不建议使用,可能会隐藏错误。

 

try:

print 'A simple task'

except:

print 'What? Something went wrong?'

else:

print 'Ah... It went as planned.'

If you run this, you get the following output

 

try:

1/0

except NameError:

print "Unknown variable"

else:

print "That went well!"

finally:

print "Cleaning up."

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载