016异常处理

异常处理


一、什么是异常

异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下

1.1语法错误

语法错误,根本过不了python解释器的语法检测,必须在程序执行前就改正。

# 语法错误示范一
if

# 语法错误示范二
def test:
    pass

# 语法错误示范三
class Foo
    pass

# 语法错误示范四
print(haha

1.2 逻辑错误

# TypeError:int类型不可迭代
for i in 3:
    pass

# ValueError
num=input(">>: ") #输入hello
int(num)

# NameError
aaa

# IndexError
l=['egon','aa']
l[3]

# KeyError
dic={'name':'egon'}
dic['age']

# AttributeError
class Foo:pass
Foo.x

# ZeroDivisionError:无法完成计算
res1=1/0
res2=1+'str'

二、异常的种类

在python中不同的异常可以用不同的类型(python中统一了类与类型,类型即类)去标识,一个异常标识一种错误。

2.1、常用异常

  • AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x

  • IOError 输入/输出异常;基本上是无法打开文件

  • ImportError 无法引入模块或包;基本上是路径问题或名称错误

  • IndentationError 语法错误(的子类) ;代码没有正确对齐

  • IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]

  • KeyError 试图访问字典里不存在的键

  • KeyboardInterrupt Ctrl+C被按下

  • NameError 使用一个还未被赋予对象的变量

  • SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)

  • TypeError 传入对象类型与要求的不符合

  • UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,导致你以为正在访问它

  • ValueError 传入一个调用者不期望的值,即使值的类型是正确的

2.2 其他异常

  • ArithmeticError
  • AssertionError
  • AttributeError
  • BaseException
  • BufferError
  • BytesWarning
  • DeprecationWarning
  • EnvironmentError
  • EOFError
  • Exception
  • FloatingPointError
  • FutureWarning
  • GeneratorExit
  • ImportError
  • ImportWarning
  • IndentationError
  • IndexError
  • IOError
  • KeyboardInterrupt
  • KeyError
  • LookupError
  • MemoryError
  • NameError
  • NotImplementedError
  • OSError
  • OverflowError
  • PendingDeprecationWarning
  • ReferenceError
  • RuntimeError
  • RuntimeWarning
  • StandardError
  • StopIteration
  • SyntaxError
  • SyntaxWarning
  • SystemError
  • SystemExit
  • TabError
  • TypeError
  • UnboundLocalError
  • UnicodeDecodeError
  • UnicodeEncodeError
  • UnicodeError
  • UnicodeTranslateError
  • UnicodeWarning
  • UserWarning
  • ValueError
  • Warning
  • ZeroDivisionError

三、异常处理

为了保证程序的健壮性与容错性,即在遇到错误时程序不会崩溃,我们需要对异常进行处理

3.1 提前预防

如果错误发生的条件是可预知的,我们需要用if进行处理:在错误发生之前进行预防

AGE = 10
while True:
    age = input('>>: ').strip()
    if age.isdigit():  # 只有在age为字符串形式的整数时,下列代码才不会出错,该条件是可预知的
        age = int(age)
        if age == AGE:
            print('you got it')
            break
            

isdigit() 方法检测字符串是否只由数字组成。

输出结果:

>>: nick
>>: sdkf
>>: 2
>>: 10
you got it

3.2 之后预防

如果错误发生的条件是不可预知的,则需要用到try...except:在错误发生之后进行处理

#基本语法为
try:
    被检测的代码块
except 异常类型:
    try中一旦检测到异常,就执行这个位置的逻辑
#  举例
try:
    text = input('Enter something --> ')
except EOFError:
    print('Why did you do an EOF on me?')
except KeyboardInterrupt:
    print('You cancelled the operation.')
else:
    print('You entered {0}'.format(text))

输出结果:

$ python try_except.py
Enter something -->     # Press ctrl-d
Why did you do an EOF on me?
$ python try_except.py
Enter something -->     # Press ctrl-c
You cancelled the operation.
$ python try_except.py
Enter something --> no exceptions
You entered no exceptions

1.异常类只能用来处理指定的异常情况,如果非指定异常则无法处理。

s1 = 'hello'
try:
    int(s1)
except IndexError as e:  # 未捕获到异常,程序直接报错
    print(e)

2.多分支

s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)

输出结果:

invalid literal for int() with base 10: 'hello'

3.万能异常Exception

s1 = 'hello'
try:
    int(s1)
except Exception as e:
    print(e)

4.多分支异常与万能异常

  • 如果你想要的效果是,无论出现什么异常,我们统一丢弃,或者使用同一段代码逻辑去处理他们,那么骚年,大胆的去做吧,只有一个Exception就足够了。

  • 如果你想要的效果是,对于不同的异常我们需要定制不同的处理逻辑,那就需要用到多分支了。

5.也可以在多分支后来一个Exception

s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
except Exception as e:
    print(e)

6.异常的最终执行

s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
#except Exception as e:
#    print(e)
else:
    print('try内代码块没有异常则执行我')
finally:
    print('无论异常与否,都会执行该模块,通常是进行清理工作')

四、try...except总结

  1. 把错误处理和真正的工作分开来
  2. 代码更易组织,更清晰,复杂的工作任务更容易实现;
  3. 毫无疑问,更安全了,不至于由于一些小的疏忽而使程序意外崩溃了;

五、抛出异常raise

5.1 自定义异常

class ShortInputException(Exception):
    '''A user-defined exception class.'''
    def __init__(self, length, atleast):
        Exception.__init__(self)
        self.length = length
        self.atleast = atleast
try:
    text = input('Enter something --> ')
    if len(text) < 3:
        raise ShortInputException(len(text), 3)
    # Other work can continue as usual here
except EOFError:
    print('Why did you do an EOF on me?')
except ShortInputException as ex:
    print('ShortInputException: The input was {0} long, expected at least {1}'\
          .format(ex.length, ex.atleast))
else:
    print('No exception was raised.')

输出结果:

$ python raising.py
Enter something --> a
ShortInputException: The input was 1 long, expected at least 3
$ python raising.py
Enter something --> abc
No exception was raised.

六、断言assert

assert 1 == 1
>>> assert 1 + 1 == 2                                     
>>> assert 1 + 1 == 3                                     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert 2 + 2 == 5, "Only for very large values of 2"  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Only for very large values of 2

如果 Python 表达式求值结果为 False, assert 语句会抛出一个 AssertionError 。

七、try...finally

假设你的程序正在读取一个文件。如何保证无论是否发生异常文件对象都能被适当的关闭?这可以通过finally块做到。

注意你可以同时为try块关联exceptfinally块。如果你希望同时使用两者则必须将一个嵌入另一个中。

import time

try:
    f = open('poem.txt')
    while True: # our usual file-reading idiom
        line = f.readline()
        if len(line) == 0:
            break
        print(line, end='')
        time.sleep(2) # To make sure it runs for a while
except KeyboardInterrupt:
    print('!! You cancelled the reading from the file.')
finally:
    f.close()
    print('(Cleaning up: Closed the file)')

输出:

$ python finally.py
Programming is fun
When the work is done
if you wanna make your work also fun:
!! You cancelled the reading from the file.
(Cleaning up: Closed the file)

注意ctrl-c会导致抛出KeyboardInterrupt异常,随后程序结束。但在程序结束前finally会被执行因此文件对象永远都会被关闭。

猜你喜欢

转载自www.cnblogs.com/FirstReed/p/11735474.html