python自学Python原教Python爬虫作业

Python爬虫day3.2—python异常处理

2017-12-26  本文已影响11人  techLee

异常处理概述

python程序在执行的时候,经常会遇到异常,如果中间异常不处理,经常会导致程序崩溃。比如爬虫,如果不进行异常处理,很可能虫爬了一半,直接崩溃了。

#异常处理
for i in range(0,10):
    print(i)
    if(i==4):
        print(iuuuu)
代码运行异常
异常处理格式:
try:
    程序
except Exception as 异常名称:
    异常处理部分
#异常处理
'''
#异常处理
'''
异常处理格式:
try:
    程序
except Exception as 异常名称:
    异常处理部分
'''

try:
    for i in range(0,10):
        print(i)
        if(i==4):
            print(iuuuu)
    print("hello")        

except Exception as err:
    print(err)
代码运行结果
#让异常后的程序继续
for i in range(0,10):
    try:
        print(i)
        if(i==4):
            print(iuuuu)
    except Exception as err:
        print(err)
print("hello") 
代码运行结果

注意代码缩进
注意代码缩进
注意代码缩进

上一篇下一篇

猜你喜欢

热点阅读