zoukankan      html  css  js  c++  java
  • 异常处理

    --raise
    >>> raise IOError('This is a test!')
    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in <module>
    raise IOError('This is a test!')
    OSError: This is a test!
     
    --try except
    def get_age():
    while True:
    try:
    n = int(input('How old are you? '))
    return n
    except ValueError:
    print('Please enter an integer value.')
     
    运行结果:
    >>> get_age()
    How old are you? 30
    30
    >>> get_age()
    How old are you? aaa
    Please enter an integer value.
    How old are you? 28
    28
     
    --捕获多种异常
    try:
    语句
    except(ValueError, TypeError):
    语句
     
    try:
    语句
    except ValueError:
    语句
    except TypeError:
    语句
     
    --捕获所有异常
    try:
    语句
    except:
    语句
     
    --清理操作
    try:
    语句
    except:
    语句
    finally:
    语句
     
  • 相关阅读:
    ES6-Object
    ES6-Iterator
    ES6-Generator
    ES6-fetch
    ES6-Function
    ES6-Array
    ES6-Class
    SQLite使用事务更新—by command
    交款功能代码
    SQLite本地事务处理
  • 原文地址:https://www.cnblogs.com/john2017/p/6371670.html
Copyright © 2011-2022 走看看