错误和异常
当出错时,你通常会看到Python的”异常”(exception)。Python甚至试图指出什么原因导致异常。你经常见到异常的名字, 如NameError
或ValueError
(参见Python参考手册py中的完整异常列表)。
sage: 3_2
------------------------------------------------------------
File "<console>", line 1
ZZ(3)_2
^
SyntaxError: invalid syntax
sage: EllipticCurve([0,infinity])
------------------------------------------------------------
Traceback (most recent call last):
...
TypeError: Unable to coerce Infinity (<class 'sage...Infinity'>) to Rational
有时候交互的debugger对于检查错误非常有用。你可以使用%pdb
打开或关闭它(默认是关闭的)。如果引发了一个异常,并且debugger是打开的,会出现提示符ipdb>
。在debugger中,你可以输出任何局部变量的状态,检查栈的上下文。
sage: %pdb
Automatic pdb calling has been turned ON
sage: EllipticCurve([1,infinity])
---------------------------------------------------------------------------
<type 'exceptions.TypeError'> Traceback (most recent call last)
...
ipdb>
在提示符ipdb>
中输入?
可以得到debugger的命令列表:
ipdb> ?
Documented commands (type help <topic>):
========================================
EOF break commands debug h l pdef quit tbreak
a bt condition disable help list pdoc r u
alias c cont down ignore n pinfo return unalias
args cl continue enable j next pp s up
b clear d exit jump p q step w
whatis where
Miscellaneous help topics:
==========================
exec pdb
Undocumented commands:
======================
retval rv
按Ctrl+D或输入quit
返回Sage.