Python的异常处理

异常处理
由于我经常爬虫,会因为网络,字符编码集等原因让程序崩溃,从而导致代码停止。

为了解决这个问题,我们可以使用异常处理,从而使程序跳过异常,保证程序可以不停止运行。

异常处理的格式(这个是最常见的一种,也是最实用的一种)

try:
    程序
except Exception as 异常名称:
    异常处理部分



示例

try:
    for i in range(0,10):
        print(i)
        if(i==4):
            print(ij)
        print("hello")
except Exception as err:
    print(err)


异常处理过后

#让异常后的程序继续
for i in range(0,10):
    try:
        print(i)
        if(i==4):
            print(ij)
    except Exception as err:
        print(err)

原文地址:https://www.cnblogs.com/heian99/p/11972258.html