python中至少包括两种错误:语法错误(syntax errors)和异常(exceptions)。

1.语法错误

   语法错误,也被称作解析错误

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbAAAABSCAIAAAC61xZAAAAKA0lEQVR4nO2dzbWzOBKGK5sJYRTCJOLKou0IWjuvenazZK3dLEji4/RpRTA3Cc2CQugXBAbse/t9jo+PjaWSVKAXqcCCHAAAAOecc/TuCixge6V6O76/uy4AfG/Qm5o4QxBNRzxMX760ehDdgy2tWP1kI+970cq9kPvQsowjckpXf7d66df2NG+h0c9WrzhhK8c65Lyj5crjsMz23lTpuVHv/nm8JIgD04PkdZ8dbfub/ooS7nHioPXX9O4Ze9T4Wt2xVjtiUSJi56xTbRl3IGU555wzXD76a9v9r3rt1N2S5nrCtq+y7IStHOiQTa34EMvtFHtTA3nPzXv3Hka1fRB17z1PZLw4QvT+WnbTQWcVMx9YWq3pmnVq0j6tpNv4DwcTlOWc48og6KzSE8yuUvblcmnbV7nICQsUW5q04kAfnmf5Cs4ZDxq+q1EuPm68eYggmo54MKbesGOaHYqgNc4fIVzqkOEkxScopnydsKxxOMClEcFJpR9Syu66bZ0MXuOErRVIWnGgD8+zfAlnCNZHxzFfF0SKAg2Gm+IOVj+J7rTFL9Yp5RqTR5MUnzGwoNU09fbJzDwTl4m5cmb6UM2VTYjGERAHVZXA2c0p5f5gsaBuTpGc9g1HpcjUXrmbclRLU6qP4TmeQMr9t9IKnyyyTLH9Bm9EbbdOkbvdHJHTumzH7wsVlK6Vo2BLtazETlCr2SFZGr9llKRyS7Nwx7I35sDLtJvsOyyHJD70dkJ/7tWfuOemvdt0RHfiXqsH0dMPYld6t+mmLD91ypzoXX5WSbb4U4TpVHM8wjQHYhLp9BnzD2EWdi4M0ufXQIoVSMqavs6ndCOH4xzQ5GmjieU4NG6mg3s5TV6fkp2kFXMDa5anVhiueyNr+3gOYA4G7xWvmoYtUVl5ffJq52X5uepCS7NWlL2RW+bses6VlhNKPpRhqXW3ly46rfZl04kUBtGz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iiEthdhyN9ZpHiIbrk1bT+yH5nOYkzwMAZqqCOIexkgGF4XkG7W+UiYJltlfxDTQSNbt1in775+0/MquNAnPRbTdBOCwPkD11UJsshlhIs6qSQUYfayu3K5lpBu26K/3nFLArVntW0jEXc1e9x0gcmN6EdILnAQAhm2/MHhgd6T3A8wCcTbsgBtc6waXA8wBcBP66BwAAAgQRAAAECCIAAAgQRAAAECCIAAAgQBABAECAIAIAgABBBAAAAYIIAAACBBEAAAQIIgAACBBEAAAQ/g+qedX2RuGARwAAAABJRU5ErkJggg==" alt="" />

  语法分析器指出错误行,并且在检测到错误的位置前面显示一个小“箭头”。 错误是由箭头前面的标记引起的(或者至少是这么检测的)。错误会输出文件名和行号,所以如果是从脚本输入的你就知道去哪里检查错误了。


2.异常

  即使一条语句或表达式在语法上是正确的,当试图执行它时也可能会引发错误。 运行期检测到的错误称为异常。此时会停止代码的执行,而大多数异常是并没有被处理的,所以会得到这样的异常信息显示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAegAAABSCAIAAAAQFND8AAALqklEQVR4nO2dS5LzqBKFWcnt4d0CGzK7aNcKLjOPegkaM+uBNvE7bjQr6NoEPRAgHslDsiSb6vNFhaNKJTIhbY5xSiaZuRQ1MXZn4rmx2VNsaPUUfNabe/bD+Jb8Kw51fqSn1X60fAj1uhkAQA4jjz4F+2L2537w9FPTHl3obgW9WMmD1hPGfU8QAd5BATgJWrhnqfwEfkr5faTHc4X7Kdh0hG5/S3mIeL3V+6XCTfRZiaPf9QEAxhRX3MqsE1ipQyXsVOE+arV42Krzrd6vFG6yVXLwvVEF4OdAC7cxJppmStjUp5aPIAe6pES/WLDI1fKRJFhsE/+pWU1MTIJ9MfaQ9lCHHbf8byS71z5/S35n/MHZnd8mvvrK+rPmhe5cfrvB2iOkr0I0csutaNiM/yz5Go2m94JlIoYHCXduuTtiUGoATqFPuO2fbqKmJ/hf9MyX6R0ecSJl26ppkQAtH+uRph0v3HUhCPusJsZn7R/j/hDeK2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+9uuIuFoRzrZS41+KzQ7hzO7kvAAABLdzrXYDhh9agMNjv//nti7FJCP9oE5dxDje9HTAvXUYWM3N2whw3VW+sebmMKoG2fRS9nFQ4LS3tZnwVt3IPC2MncvfdBeGEmPxpSXzcn+TTWitHl8c59wUAyMAXcAAAYDAg3AAAMBgQbgAAGAwINwAADAaEGwAABgPCDQAAgwHhBgCAwYBwAwDAYEC4AQBgMCDcAAAwGBBuAAAYDAg3AAAMBoQbAAAGA8INAACDAeEGAIDBgHADAMBg/AN5WhHi2FigTAAAAABJRU5ErkJggg==" alt="" />

  打印错误信息时,异常的类型作为异常的内置名显示。对于所有的内置异常都是如此,不过用户自定义异常就不一定了(尽管这是一个很有用的约定)。标准异常名是内置的标识(没有保留关键字)。

  python的标准异常有以下这些:

异常名称 描述
BaseException 所有异常的基类
SystemExit 解释器请求退出
KeyboardInterrupt 用户中断执行(通常是输入ctrl+c)
Exception 常规错误的基类
StopIteration 迭代器没有更多的值
GeneratorExit 生成器(generator)发生异常来通知退出
StandardError 所有的内建标准异常的基类
ArithmeticError 所有数值计算错误的基类
FloatingPointError 浮点计算错误
OverflowError 数值运算超出最大限制
ZeroDivisionError 除(或取模)零 (所有数据类型)
AssertionError 断言语句失败
AttributeError 对象没有这个属性
EOFError 没有内建输入,到达EOF 标记
EnvironmentError 操作系统错误的基类
IOError 输入/输出操作失败
OSError 操作系统错误
WindowsError 系统调用失败
ImportError 导入模块/对象失败
LookupError 无效数据查询的基类
IndexError 序列中没有此索引(index)
KeyError 映射中没有这个键
MemoryError 内存溢出错误(对于Python 解释器不是致命的)
NameError 未声明/初始化对象 (没有属性)
UnboundLocalError 访问未初始化的本地变量
ReferenceError 弱引用(Weak reference)试图访问已经垃圾回收了的对象
RuntimeError 一般的运行时错误
NotImplementedError 尚未实现的方法
SyntaxError Python 语法错误
IndentationError 缩进错误
TabError Tab 和空格混用
SystemError 一般的解释器系统错误
TypeError 对类型无效的操作
ValueError 传入无效的参数
UnicodeError Unicode 相关的错误
UnicodeDecodeError Unicode 解码时的错误
UnicodeEncodeError Unicode 编码时错误
UnicodeTranslateError Unicode 转换时错误
Warning 警告的基类
DeprecationWarning 关于被弃用的特征的警告
FutureWarning 关于构造将来语义会有改变的警告
OverflowWarning 旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning 关于特性将会被废弃的警告
RuntimeWarning 可疑的运行时行为(runtime behavior)的警告
SyntaxWarning 可疑的语法的警告
UserWarning 用户代码生成的警告

  当然,异常的触发并不绝对导致程序的终止或崩溃,因为我们可以预见性地处理这些异常。


3.异常处理

  我们可以使用 try/except 语句来捕捉并处理异常。

  try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。

  如果你不想在异常发生时结束你的程序,只需在try里捕获它。

   try 语句的执行顺序:

  1.首先,执行 try 子句 (在 tryexcept 关键字之间的部分)。

  2.如果没有异常发生, except 子句 在 try 语句执行完毕后就被忽略了。

  3.如果在 try 子句执行过程中发生了异常,那么该子句其余的部分就会被忽略。如果异常匹配于 except 关键字后面指定的异常类型,就执行对应的 except 子句。然后继续执行 try 语句之后的代码。

  4.如果发生了一个异常,在 except 子句中没有与之匹配的分支,它就会传递到上一级 try 语句中。如果最终仍找不到对应的处理语句,它就成为一个未处理异常,终止程序运行,显示提示信息。

while True:
try:
num = int(raw_input('请输入一个数字:'))
print num
break
except ValueError:
print '非法字符' print '我在try之后'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAABrCAIAAADhOBmtAAAFoElEQVR4nO2dwXXrKhBAKUsNfcpwOtDOVWT7tXcV3qgDN8FfOC+PD8xokPDxRLl34SNhGJHD1YAkOw4JwDHh3R0A0EBQcA2CgmsQFFyDoOCabUFDKOvUJc3yZrUQgtR8H3q0/F2l5tguwUBMA/McvyBQVCuaKAGHoB+ifu2NA+9lz6g0c2ruqyVvDRFi0zylJ9L5Vp94NpZ4CeEjhMs0PzqbgkzfMFjSZ5FukyDQcUG75EvqWXS0e495usTluX2P4TqvHY1Bw7QGrY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lbrF/5SPIHLotqRRMl4BD0Q9SvvXHgvewZFemRkvHRjl6+oyfG1FgneJ3u3txjvHc3Ap2+YbCkzyLdJkGg44J2yZfUs+ho9x7zdAnhMs2Prr8Atun4NFNeUtdplisxu+pLEZobeR1LNcuxDK2WeBnzbRbIGbMG1afI40IXbaXgtZHNkjqa1PPmEds85una/goAHKFD0NQa3XpXepVqFuU6St+UDuud2Yxm4h7DJwl0PN2CKsbUb0lJq7m98w8w3zzaFPRIZ9bbNN1IoOMZk0ElKd8laPMsKjpjz9ZS8s5ZPgOX8K9gcAZNsqxFKKWkC6l586Talz43BH1ewn+E8BFIosOxCrqZC5OcNaUELMXpwi6WPsUfz+XwCvZP8UoGLZroZtSRu1Cm+LpXzSbN2QCcwKiAaxAUXIOg4BoEBdcgKLgGQcE1CAquQVBwDYKCa6yCCv9QFuC1GARV/108wEsxZtBhv84B0AWCgmsQFFyDoOAaBAXX2K/iuZKHN8CNenANgoJrEBRcg6DgGgQF1yAouAZBwTUICq5BUHANgoJrEBRcg6DgGgQF1yAouAZBwTX/ARApqlomODIOAAAAAElFTkSuQmCC" alt="" />

  示例中,当我输入一个无法被转换为数字的 @ 号时,本来应该触发ValueError异常的。但是进行了异常处理了之后,当我输入 @ 号时,异常被捕捉到了,同时执行了except中的语句,并执行了try之后的代码。但因为是在永真的while循环中,所以又执行了一遍,这时我输入了正确的数字,所以try中的语句能继续执行下去,又遇到了break,循环退出。

  一个 try 语句可能包含多个 except 子句(至少一个),分别指定处理不同的异常。至多只会有一个分支被执行。

try:
  '''尝试各种操作'''
except IOError as e:
'''捕捉到这种异常,就执行这里的代码'''
except ValueError:
'''捕捉到这种异常,就执行这里的代码'''
except:
  '''如果发生的异常不是上面的两种,那么剩下的异常都统一这样处理'''

  最后一个 except 子句可以省略异常名称,以作为通配符使用。但是正以为它能捕捉任何的异常,所以一些在意料之外的异常也会被掩盖掉。

  我们也可以用一个except 子句捕捉多个异常,做统一的处理:

except (RuntimeError, TypeError, NameError):
pass

  这时又有同学好奇 except IOError as e: 是什么意思,下面看代码演示:

while True:
try:
num = int(raw_input('请输入一个数字:'))
print num
break
except ValueError as msg:  #等价于:except ValueError,msg:
print msg

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS8AAABQCAIAAADHv2QvAAAGpklEQVR4nO2dQZLjKgyGdSwu1Byj+wbscopes3uLnCKL4Qa5Qi88CzttDJIAw8S8qv+rVCpDQAjCL4HdmdACAJgDutoBAMAG1AjALECNAMyCqEai9K28hC1nqxGR1PwcurX4XaXmWJcA6KS8pkkgqZY0UQwOQe8if261A8D7aViLbLaMxVmTkYas/qLMFE+k4JJHmTq8/ST6Ivo07tnYFIAjVYuvJjEmiXQR1NKvxialLWrI6HXv6cyn9evrh6WbCw2NAUjR0ku+1vM6bLlis6m+ZIF9EdepqVbTl9oquJtxzyXcDd1ciJUJwCm6zo36Tq9fvUlbyXguP7YktyZ5zvaY8nTm5sLi7c183MPrnwCcp2rbpifJeLnnz1LNpFxH8U1xWHemaK3Aw9K3Xx6WbsY9gruRfTTbACCmVo2KPPK3pHTEvj7pd/V9i6IaTzrzsPTtw93QF9EXmTvyIuilKzdKCrxKjWzISJypz8NSWt54OrNfR93OkAD0MCY3LrIyE1NKSZvfpTsoleLX3yo4+bBrYsTtDTCEghqLWW6R86GUWiU7TdSrSN+p9mdpAEbRvFNVcmPSRJdBbrkJZaeae8U2YfM8ABeCtQjALECNAMwC1AjALBTU+GMMHnjg8WPMFGp8gxMATA7UCMAsQI0AzALUCGoI7mP97sjvC8DiLRW/8ibWmUGN/seYzu/sBWeIiNJBssNe6/LT0WKnF6GvzqoK50fhrdklGJw5Y0bv3dvy+GrqXM5hqlrrzKDG8GPMiHCbf95SIC+ujEo7/dQoJDizVuoMCmdHkcmvZsW1996VVYbgbWz8FQIbxhqc4WqvYYSIjPsj1VmWZQ41jnKi/qNqVSNDcG7Esrh+CSbk42K6H+TRsa9Lp2ITzG58D0GnYs/B8qv9b1iVgBrP2Rm1LGZTY94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A8IVUswAAANRHNQsAAEB9VLMAAADURzULAABAfVSzAAAA1Ec1CwAAQH1UswAAANRHNQsAAEB9VLMAAADURzULAABAfVSzAAAA1Ec1CwAAQH3u1ez/AUdAtHA9j+fLAAAAAElFTkSuQmCC" alt="" />

  非常多,一行显示不完,不过多数是私有方法,我们重点关注后面的args,和message。

print msg.args
print type(msg.args)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXMAAAApCAIAAABIul7gAAAErUlEQVR4nO2d25ncIAyFKYuGVn2kAaqJ3lLJUszkwcZgrgJrLrs+/8N8Ey+WBJgD2B7FPAAAQBvz7gAAAL8QKAsAQB8oCwBAHygLAECfobJ49+X88d1a57fPJ8cF1Dh6MO1KUMJkiFfKDMfFHVt+oCxMSWN5Z4n3z+Pv484Y4J01xpjcTqsLy5ILdq7S8HWxaIf1WlR6UNk707h+kjJv59RUE2Uq46JgseV/MF1lOTcHO+fDZyyhI8bltduyPLzKhXauIxnt3tmt0EWBW61FcUFLRs+89+XZXg2m1HiQ84m6NlYcmyQaY6z7bpSpjYtqhPda6feU5bnXwqKrWWWp4J3TqNb7h1NGWa+Ke6WIzr7e2hT74I/Go5xeHM1MhzYdU8Qla3datnSU5ZUt8Upl0arXpylL6avqXSWkzMjbmyI1nq4sUmmZDWC345011nmF+O8lLReUJZ8r9r20czasQ+PumimWDEvV1PrZVzELxYPWdueOkZ1jcZse3wNKQjbE+0FxzAXHKbO+KnUatHOjXtPKwmQMEZn9M2zjxm3IlMVTN77ZjmXKVo2280L9xUdaqdb3WbZzvbNEzBq6AGXZWZiImOKgIX4kO/S4DT1O6V8B2ZEwEQ1WpZJ5u2E5zm9M+ZUsjHkYT1KLpi+JnaKdRTUdh701AROxZ/adUyTxlH4NcVb3rFVrt8ny3mkZf5KyMBlrHac7o1WgLAHBLrW1Kj6+hAdy2a58PLsOLUviEVquzMC5C1HMw3gkY2nNTt1UpQf7DymYrHPFcxyV3qmVqbdqqrVl7/SNP9q7oVk2O0zW9laUU/ZuJCz9Z0PjfhFcYelGKP3TafUhXlmEmU8WT/cIc6J9mxPv+6cMYh7GU12zaCvLXq/9WNaDo8ubadto1ZdsxZHdl1BZGnWPrXqsbLOZaQvdy9YsT7mDO2Or3gg3E5ap91mKv51nk/DvbQkZZb54ZrlPS19f1hjr/iX3I8q7BrEz9tsLlAnVgRfaKd8wSbf7ySn54OrH3A0olqncZxndRZC3c+3Nmdn3WZiIH8leaNyGrXhyw85xfj/p3KrfaZNVe0fUPsk5A30cUwtoSPudlzsx9Q4u+IngHdxXUn3n5Y4tj98NAQD0gbIAAPSBsgAA9IGyAAD0gbIAAPSBsgAA9IGyAAD0gbIAAPT5BGV55S+17vWrMADexWcoi0a2LVlayLtl9gLgPQyV5ZwGUB/dfN39JUkj2+CTawjADekrSz5QlfI8iljytbbZwRYJAGU6ylL+PPPzb4gsR4hdEgCaNJUlH2ppCh5Df9MfzIevf448h628AULOvoKHypfcV6kscu/3+507AM+jpSySRIdRfELGnjIjoTDb4DCAaja2zFd51pR37IkAUGMqD25528WGvKlZgdP4n8+dU/rq5HnsZGyUe4esAKDJ1B3cMh9i9p8WlhkJhdkG266zfIgxW2Xpqwx4JdchAOA6U0+da6+MnPYZTKbIRzzMNlgn9xX+5wxK7rScfFWyVcq846kzAOpcfVPuPCw//+ERAOAVLCtLJZnyfuglj29f6QsAMMsnvN0PAPhtQFkAAPpAWQAA+kBZAAD6QFkAAPr8BwAhw/fDS5mgAAAAAElFTkSuQmCC" alt="" />

  看来是将错误信息转换成字符串后保存到元祖中。

print msg.message
print type(msg.message)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU0AAAAoCAIAAAD13D/OAAAEJElEQVR4nO2dW5qkIAyFWRYb6uxjNsBqJm+zkmExNQ+oIESISI0Wdf6H/qptTAJyuKiVNi8AwOyYuwMAALwd6ByA+YHOAZgf6HwCvPtxfvcBiDAZ4gFlPo+mzgdU2ztrjDG5HdFyKCt7PGPnKge+Lhat0F8LJhvF7Z3tMV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o9K6XLkQOvpeO+3Bl9qLsn6iU+YOUuYFyv0WOodJ7X1YmAL6LjudqUvai3XqcyRSZ9Zq5gQTElD6Jd2VWJjxXA1/PmPdk9kLC5AnAs7iocyEt4HIID7EAeAzPee8VAPAuoHMA5gc6B2B+oHMA5gc6B2B+/gEbzF85KP9f7AAAAABJRU5ErkJggg==" alt="" />

  看来就是把错误信息转换成字符串。

  

   try 除了可以配合 except使用之外,还能配合 else 和 finally 使用。

  else子句只能出现在所有 except 子句之后。当 try 语句没有抛出异常时,需要执行一些代码,可以使用这个子句。

  不管有没有发生异常finally子句在程序离开 try 后都一定会被执行。当 try 语句中发生了未被 except 捕获的异常(或者它发生在 exceptelse 子句中),在 finally 子句执行完后它会被重新抛出。 try 语句经由 breakcontinuereturn 语句退出也一样会执行 finally 子句。

while True:
try:
num = int(raw_input('请输入一个数字:'))
print num
except ValueError, msg:
print msg.message
else:
print '没有异常的时候才会触发'  #当try中使用breakcontinuereturn 语句退出时,不执行
finally:
print '无论如何我都会执行'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMUAAABSCAIAAABrOYdcAAAE60lEQVR4nO2czXHjMAyFWZYrUhlOB75tFTn7nipySQdpgnvYmYxCPDyAFGayit53yCgUfyDiE2xrN25diDraTwcgfhXySVQin0Ql8klUIp9EJfJJVPLNp9ZGvWwLbIfdWmve8DX4bPuzpGdtSGIAC9Qchm7DELxAXf74Evbn7DziOMHOwoq11ytTFUryF4pCIvFuD3ufZHnftvfpQVeApScsTkMx606+j/s05Uqn0h8N7/Nxu7d2vz0+p67gKoDcDC3jgESGjvT3ZoAH+z6Zbpm1EqOe2317zk59DabfP/HXi+P+DWO9ya1AsMXO5kUOV8R8Pm5/Hh8L13MB3A90vFDtE2Z/ej2Hdg6IlTZmgglnS/G+tVeVJwzziSTYnvJKAjxejDX9FCD06UgwH2+325vKE2a6PnkO/ZRPUPohmHwt9Erjnudr04c7j/X61H23hqlIy1ys0fOIpL78FAvy34e7l9ZemkoU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n/XTSkZtAeAY1p2n8G5rOgr1ipUiW+XYKC3FWuqkLB52aYAmwU+in82M9U19ecwPqzG5e1UYhfTkuOyj0Q8Ah5FqIH0pSKVlzFZCuQiz7Wz1E5yrtZQmQ91WfdX0ADiPaW7JWlLFJtQZQTm0kbW0hJOhwpbN6HOhFwC9YCYBMBfQJABzAU0CMBd/AU8PbEiE0xWXAAAAAElFTkSuQmCC" alt="" />


4.手动触发异常:

    raise 语句允许程序员强制抛出一个指定的异常。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAP4AAAB0CAIAAAAq3xi4AAAIHUlEQVR4nO2ca5LkKAyEfeu+xpyjL8n+mF0vhR4kT+NSftHR4aJAEjgRsntiriS4/kO2q92sdmkZAbRpeQfjvD6xHOUfrc7IjKp9/OHg3P1GORGrc9XRiuBbDaq3yfKlW2jy6nzcjON95D6NG5kVDFkNlLTABLONdSE9Oy+yE95jEhRK/zX8X9r+XtfvC26cGuc5J2oZx48WGd44h59plv9cf/7+FNdTbM4yiFBWd7/lx1tn+bXfuRiyJGzN7CHqHxL6QvXPI1fnLJnmdvZLX9eTrXW1Z/67OqQPy+a50k/fpf7V0t8AohVc+mqfzRXUCeo3I5ip/p/r3xrmvsjb/ZaiHen8yV3tJJGti11h1TCyUd1LRWXlNFo21Z5pQPqy+FH7p+wQcEqpvEV2dnwp3k+Wflqk/rxFvfY/3tfO8E9U6Ts6Bns6m0QGILtZ1+p5Akq/qODzr9T+cnjxlXWS+C6gM+dk6U+ueSx1qpnbyvrqwFrit7J+0hK/7KmeBlYFpQpX3QzV65zBggfP+sW1tZ2U9zb2xtOjPVb682t9VZ1qCrc6p4ZMnyNrD9meXzhZX45F2l8t/aovv6fFodJf8oyrJnLr2wUFT7Kfd/sKHtmYlkk/AVpZLf1q1vfdfXQ+QPdp03t9qzKRRYtaxlSH21iFSv5VUc9Yj6pWCeQ/ADQ9Oqs9/+LLxXqv71QsRXFv/VnAGVt9IDajPVP65FgOUcwg58zilDgI2QylT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KfpPOLLp8ClYTuN2zkiiLWo71vGbarX66h6xEMqXn0gQ/qwbFL6m1gt/Q2A7xa7+4NDJnKA+ndlLPVgVY9mq0DEO8uxSSQ5OdypYZwSQhpMYrM1zV32TAPSd5ZIdQHOHZwRGO1D7IrAv53VQlZeTzzfi0awp7NJZADg3EGZqvhbt2pTPRvxOAfPnCfYKH2rHblVTjaqZhor60vvlvSlI1UcauTWdKrXVbOgTTCq6g7348RvhxPSXh6VfpOAEqYS34t/gFSzflOETuOLpA/G0yrl0NLHb1X1riBZPxlnurTpSB+pBJwAqkOciVS1slr61azfF+pzbAnCOgrlKWlVF/5wxPX90fKex3B/axnB48SDt3rKyP1pVtfTmZH/VeuMnGgP4IggCMQZihnlmFmcEgchm6H0SVAofRIU+0kFf2qpeDAeCh/Enyb4LE5ejvG3ift6jpOu9/FLwUMqXvsgQ8gb2C79Q2jdjfIoIC/H+NtEkeRS7d12YcEqIaTBJDabWlRYb5fVnghWeOBrdavyaYqThdOjYNJ3dAz2dDaJDMBJsaqqOvC3rtrfCtKfuxrngRVgPOCsn4y/jErpS0lZFRQisuRKqhvHDp71i2trO1nbm4n/UWydVYuWataXY5H2V0u/6svvSTaCSX+84JGN6aulX836vjuyHqCkKb4qTur7tzz3rYGyXfXiBOb3rE/6Vf/Yi6yBS0+CQumToKyV/nVd3/bfepJvgdInQSl1+aMpFW8srVP65FSGhF5VP6VPjkXX5Sz1U/rkWExdTlE/pU+OxdPluPopfXIszPokKKz1SVD4hocEhe/1SVD411wSFEqfBIW6JEGh9ElQKH0SFEqfBIXSJ0Gh9ElQKH0SFEqfBIXSJ0Gh9ElQKH0SFEqfBIXSJ0Gh9ElQKH0SFEqfBIXSJ0Gh9ElQKH0SFEqfBIXSJ0Gh9ElQ/gGLKZu2JXZljgAAAABJRU5ErkJggg==" alt="" />

  要抛出的异常由 raise 的唯一参数标识。它必需是一个异常实例或异常类(继承自 Exception 的类)。

  如果你需要明确一个异常是否抛出,但不想处理它, raise 语句可以让你很简单的重新抛出该异常:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArQAAAEFCAIAAACdIuPAAAAgAElEQVR4nO3dv2rcTNvHcZ1CTkIn4ByAi7QCNwazhY/gcakFF+6CSePGsBgXD2qyGFyk2F2IA4HFhasVJhg74MJFktcBm0C6VA/MW2hmNJc00mp35X/r7wffsNbq345vMj9dM1oF/wMAAHAET30CAADgeQl+/N8tP/zwww8//PDDj/0JFAAAgINwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEB4knAw6K529q+e4sgAAGCa+nDw5f36f/57PffCKoQDAACer6mVgy/v19fX33+Zd2HJ1X5n1ZGFBLOwOxh0sxf7+53V1dXuwNmgO7A7GXTl7wAAoDWNhhWu//uf9VJhoPnCEm/lIA8IatBd7Q7U1f7+wL4j1yccAADwYJrPOWixhFAdDmSHP+h29q882QAAADychuHg+r//KdcDmi8sqAwHxWLA1X5ntdMhGwAA8IgahIO2Zx24OcCJCb5woAcQStmAYQUAAB7M1LsVylMImi+spucdropoIJcou7xcNyAcAADwYJ75lyAx3wAAgMf2bMOBrS34hhUAAMCDebbhAAAAPA3CAQAAEAgHAABAIBwAAACBcAAAAATCAQAAEAgHAABAIBwAAADhNYYDz/Mf55bG/SA4DMLTVKkkOgyCwyA4DKKLhXaaDPV+gpZ22Ez2EEzr4/bNm3e3H82v570fb97dvNn+q77fvX0n3lLD2zfbf+WGzs/m3bn6t7N5467TosJ5KqWu9rt8bRYALOA1hoOWZfkgSpRSSiXDLCgsytlPGvcfIxxc7XdkYDrv/ZCd7r+d7bvz7OXwdmNYuaeP27cflVLf7zZ6/5yt/m48TDgonafnswAAZrEU4cA8tak70F+63B0o91lOTp3ALpRfyez9smZnYV1Xk8b9IOyHWV9uO/X0NJTX/TpDRMMgGCbZu6bLz0sOwTDRi4ZBeJomwyAYxiYc6CpFMEx0aWGYVCx0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XDgodueHOOXAtVTgorlK9cfWaAAA8iYbhQN3H0Sg04SCfolhbOVDpJPTMNxThYFrvXlqYn5KS8xn94WCBOQf+3wurF9exb+UDDFM2rl4TAIAnMfVWRvduRt0T38ehHlOIolEQjJNsPoRzQ6sAAAC1SURBVGAwitPrKDgO4/s0HgXBOFEqjUeFuxaLYw3BOFHOHEM5VOFZWDGo4V1TNQsHurCf38qoZwSsear97lI7zUDc1Vh8w11XDCG4Iw2VUx4AAHh0fAkSAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAEAgHAAAAIFwAAAABMIBAAAQCAcAAED4f7y67I1CS1W7AAAAAElFTkSuQmCC" alt="" />


5.用户自定义异常

  在程序中可以通过创建新的异常类型来命名自己的异常(Python 类的内容请参见类)。异常类通常应该直接或间接的从 Exception 类派生,例如:

class MyError(Exception):
def __init__(self, value):
self.value = value def __str__(self):
return repr(self.value) try:
raise MyError('错误信息')
except MyError as msg:
print msg.value
print msg.args
print '-----', msg.message

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAABMCAIAAAC50l+0AAAB/ElEQVR4nO3cwW3iQBiG4SmLEtJIXEbSAd1wTxV7oRjvYVdZh7FnxtKnrE2e52SMGZD86jeQiDJ/h19TeSvlfbp9y7MNuE1vpbyXy8f9747DvcLzKv/7BfBsJEWYpAiTFGGSIkxShEmKsH5SpZTVm/XGn+2lxlL1vY0n5UQCSXV3dpda7tnSfZ0cxFBSnye1e6YbSW2tsFxnKx1JnUjnVDWiWT3NjeBGsjClnkA/qcaIWu4fWedhzToXU+oJ7H4v1TimPV1MqR9iX1JbZ7oMXONMqR9i9ML3OYce7q0PG8yu+3Sm1Ekda0p1F+H4drxPmntTqj5+6+DV/fVjd004DmL0YrTcbkypuo/6ZmNKNcJV1VnsvvBt3VXfnNemV+NRjSvs6uIck/NEmKQIkxRhkiJMUoRJijBJETae1P36er1/2YAVo0ndpsu/ju7Xix8PYMNYUlVDXwqDhaGkblN5nEoru2Cex5JazUdTrJMUYZIibPC9VPVm3Ic+Nox94quaUhRbfC9FmG/PCfM3PsIkRZikCJMUYZIiTFKESYowSREmKcIkRZikCJMUYZIirJPU9PjrYi8vmT3+OeZpmVKESYowSREmKcIkRZikCJMUYZIiTFKESYowSREmKcIkRZikCJMUYZIiTFKESYowSREmKcIkRZikCJMUYZIi7DeKu3h6ZKcL6AAAAABJRU5ErkJggg==" alt="" />

  Exception 默认的 __init__() 被覆盖。新的方式简单的创建 value 属性。这就替换了原来创建 args 属性的方式。


6. assert 断言

  assert是用来检查一个条件,如果它为真,就不做任何事。如果它为假,则会抛出AssertError并且包含错误信息。

  格式: assert expression [, arguments] (expression:表达式,arguments:参数,多是字符串来说明错误)

a = 1
assert a < 5
assert a > 5, '根本没有这会事'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAABUCAIAAACUWHNbAAAM5klEQVR4nO2dTZLDqA7Huc1cgQuFY6RP8NhlNUfwmsWreotcYlKvxieYvgSzsI0xEiB/xon/v0p1JQRLQBoJ5AQpDwAA4KqodzcAAADA2/h6H9A+b/b33Y24NvgIADgvb/QBrlHqrsxr5mUvM+Oql9HPdnbLvoxfq3+mQ01LJFctp7UP49aLAQBsTt0HvIz6Uf3jvvFMds0SEyO+CqZnhA6aZBiXfUAMcMYAnJO6D3haF2zBy267qd/XB7yMarZwAb/WbmIH36r9UB/AtNmZrRcQAIANEOwDnB9tgXObWsNdfcBWa9jN1sJv1X6kD2CvSgrfO6oAgB7h/YBoxjrTh4lb+4jixV34+EdFS+/WPpIIUn9JCAu4RpnGqB+lHrYvEsgZNiWVGwNjm3+tviv90Oqub40edZH2jIGvu7a/Q2f7ElZXZjSo5Npo9HdHnlaPo1HVnpHMjOFGPoBKFo8YjD4AZ2S+D+hfDnM+rRCetE/dWYq4ZLB3/bWu6axJax9jSVVO8AFlmxK32TVKP9vwd9oeRnuh76yi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l9iY7jE7ITpsVkVh5LEiy9q/qKphm+pJtZM5j5aw8TD8AX4loYr+Malz71KMp3CsHZKgWq4/l9BlmzNPqn/H46D7PTCpk0PUXq13SQhlVm5uUFELwvrb0Ftpo2owFRlwedAIAfCiSydwfj/wysc3dLwdkUjLIae0jkvOwLUkPGV9IddESXvKCDJHyWFDZByTV6MuCkyjHedZb7er+BgDwiUgmc5TNcbSP++WATEoKWYi5FMF9HjGJl5LkN66TGHT2ZfKcfZfKzNVZvw8oLPBzzgxbAQC+EsFMbm0zZOx6mX5pv2cOSMk+IGe7w3eK5uxUGMmzYX1AzpQXTHzOlyR15PuABe6EBfsAAL6S+mRuSCz+r/1yQEbVxq8hkfsBscy7Mv9NvhtKdeW0k/sBXAsX3hNmV+K5FTe9hL4sl7PXlo1+1SVgEwDA14PJvA2xHZ8V6mELk4DMgssL2mMVcvsOuw/AV4KJvQ2z1vIFe5oz+tX1e27/MXcJX4g+lS8EAHwimNgAAHBd4AMAAOC6wAcAAMB1gQ8AAIDrAh8AAADXBT4AAACuC3wAAABcF/gAAAC4LvABAABwXeADAADgusAHAADAdYEPAACA6wIfAAAA1+VfSoGcw/pWeR0AAAAASUVORK5CYII=" alt="" />

  当然我们也可以捕获它。

a = 1
try:
assert a > 5, '根本没有这回事'
except AssertionError as msg:
print msg.args
print msg.message

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjgAAAAvCAIAAABhWyerAAAF+UlEQVR4nO2c27WkIBBFDcuE2nCM5prMGIzzYasIVVAgtrjW3h+zun0U1AMPYM/tFgAAgIbpnu4AAABADIQKAACaBqECAICmMQrVPH7GeVmWZRq6YbqzQwAA0BiWJ/8uE/UxCdU09Fv7zseVeez7Gp37nZ1p6LouGfT3+dWaHeL8Ljt6vuax77Zz7/MrRPL0fX7dN75Ey8GTX2vunoWMQaiOtkWnCzOzFf8ejzI7m5njVoudYHaw9+Y4/kO/hNbFi54cSUX5isT5Yt5X667xF+ZrfSC4hmrVYWi5LF9bn4b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ovy5drJ8Cv0oppfR94v5ivwtNCO7lteHeopy8rX4o+UYPVlqsNYfNyehU/dm2Qq/y9TRAgmOYVgBzvYwQ522rSzIi61nv7LFAAAAE+BUAEAQNMgVAAA0DQIFQAANA1CBQAATYNQAQBA0yBUAADQNAgVAAA0TVqouq7bP7jEL77DSHgqcqWdKkYAAOAm8oQqPKhdrJ26aCQ8G17cRUlegG4BADTFVaHKfehrQlVgR7smojSaC8kbAQDgKRJrF1cDLMuO+LNe0xu7BYtlu1Al5RAAAB6n5tafZRWVbCu+VNJWTtpSybLYQp8AAFom8Yy2CIN7xP1XPBtZxGhrmsiSS2wu7LD4IS6KAADQCCahEmUmvHLRhcq7LPwaUbKIUIltWYRKs4BQAQC0RvqVUqgl7lfvc1wStLsiR8KDWSsqT+ricsiiCgCgQbLfUYXaYPms2Qnbsq+ojO2K92oHUSkAgNYo+TGFuKYR1UVbYFnWW9pBTeeSiznvGpZTAACvIE+okmJgWbK42lZwe6R1twm7DiFOAAAtU/jz9PBr5OB+SlSm+M6bpo4REbKsqOK9AgCAduAZDQAATYNQAQBA0yBUAADQNAgVAAA0DUIFAABNg1ABAEDTIFQAANA0CBUAADQNQgUAAE2DUAEAQNMgVAAA0DQIFQAANM1/Yyqz/igxx2kAAAAASUVORK5CYII=" alt="" />


  为什么要处理异常?

  因为这样更保险,你不能保证所有代码都是对的,而又不想因为某处的小错误而让这个程序都无法执行,这样代价太大。所有说凡事try一下,没有什么坏处的。

  关于异常暂时就说到这里,有错误或不足的地方以后会进行修改。欢迎积极留言讨论。

18.python的异常处理的更多相关文章

  1. python的异常处理及异常类定义

    python的异常处理语法和大多数语言相似: try: try块的语句... except exceptiontype1 as var:#使用as语句获得本次捕获到的异常的实例var except块语 ...

  2. Python进阶----异常处理

    Python进阶----异常处理 一丶错误和异常   错误:       语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 ...

  3. python的异常处理机制

    异常机制己经成为衡量一门编程语言是否成熟的标准之一,使用异常处理机制的 Python 程序有更好的容错性,更加健壮. 对于计算机程序而言,情况就更复杂了一一没有人能保证自己写的程序永远不会出辛苦!就算 ...

  4. Python 多进程异常处理

    前言 最近项目用到了Python作为网站的前端,使用的框架是基于线程池的Cherrypy,但是前端依然有一些比较‘重’的模块.由于python的多线程无法很好的利用多核的性质,所以觉得把这些比较‘重’ ...

  5. Python学习:18.Python异常处理

    一.为什么使用异常处理 当程序运行的时候出现了异常,导致程序终止运行,为了解决这种情况,我们需要预先对可能出现的异常进行处理,一旦出现这种异常,就使用另一种方式解决问题,还有就是错误信息是使用者没有必 ...

  6. python基础-异常处理

    一.错误和异常 程序中难免出现错误,而错误分成两种 1.1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def te ...

  7. python专题-异常处理(基础)

    之前在学习python的时候有整理过python异常处理的文章,不够简单也不够完整,所以决定再整理一篇,算做补充. http://www.cnblogs.com/cmt110/p/7464748.ht ...

  8. [python]python的异常处理

    异常处理:首先了解异常,程序出现逻辑错误或者用户输入不合法都会引发异常,而这些异常并不是致命的所以不会导致程序崩溃死掉.可以利用Python提供的异常处理机制在异常出现时及时捕获,并从内部自我消化. ...

  9. [python]python try异常处理机制

    #python的try语句有两种风格 #一:种是处理异常(try/except/else) #二:种是无论是否发生异常都将执行最后的代码(try/finally) try/except/else风格 ...

随机推荐

  1. 加链接太麻烦?使用 linkit 模块提升用户编辑体验

    在制作网站内容时,适当地添加链接会非常用利于网站内容的SEO.加入链接的文章可以让访客了解到更多相关内容,从而提升文章的质量.被链接到的内容也能因此获得更多的访问和关注.只不过,在内容编辑时添加链接却 ...

  2. 使用Zabbix监控RabbitMQ

    一 应用场景描述 线上业务使用RabbitMQ作为消息队列中间件,那么作为运维人员对RabbitMQ的监控就很重要,本文就针对如何从头到尾使用Zabbix来监控RabbitMQ进行说明. 二 Rabb ...

  3. 慕课网-安卓工程师初养成-2-1 Java中的关键字

    来源:http://www.imooc.com/code/1176 Java 中常用关键字: 问:这么多,记不住啊......-_-|| 答:现在不需要你记住所有,混个眼熟即可,在学习的过程中,你会逐 ...

  4. VS2013 当前不会命中断点还未为文档加载任何符号

    情况:在别人那边的项目可以调试,在我这边不行.看来是电脑环境问题了 自己试过 VS2013修复下(用了1个半小时),点评:无效 网上 五花八门的都不适合 1.设置与当前版本不一致取消打勾.    点评 ...

  5. [vsftp]500 OOPS: cannot change directory

    这个报错需要检查 1./etc/passwd 用户的主目录 2./etc/vsftpd/vuser_conf 下每个用户的local_root 3.每个用户目录给ftpuser加上rwx权限,一定要有 ...

  6. html实体字符

    在html中,某些字符时预留的,如小于号(<).大于号(>),浏览器会认为它们是标签:有些字符无法用键盘输入.如果需要正确的显示它们,就需要在html源码中使用字符实体. 字符实体有实体名 ...

  7. 洛谷P1631 序列合并

    P1631 序列合并 236通过 657提交 题目提供者xmyzwls 标签堆 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 为什么不行? 题目描述 有两个长度都是N的序列A和B,在A和B中 ...

  8. MVC ckeditor的基本使用

    之前在自己的WebForm练习项目里面用到过ckeditor,时隔蛮久后,今天再一次把ckeditor运用到MVC里面,用于最近着手开发的企业站的新闻动态的内容之新增与修改. 找到的资料都说要把下载的 ...

  9. WinForm程序用使用List对象绑定DataGridView数据源

    1. 在用List<T>对象绑定DataGridView数据源属性的时候,数据源的内容不会动态更新,如果List<T>对象集合中的数据发生变化,那么数据控件的数据源是不会得到更 ...

  10. ArcGIS10.2最新全套下载地址

    http://www.tuicool.com/articles/VfaMfy 免责声明: 该链接来自于哥伦比亚大学或者牛津大学的网站链接, 下载 软件之前确保有正版的软件授权 ,本博客只是转载了网站链 ...