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