zoukankan      html  css  js  c++  java
  • 错误处理

    错误处理:
    
    在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误的代码,这样,就可以知道是否有错,
    
    以及出错的原因。
    
    在操作系统提供的调用中,返回错误码非常常见。
    
    def fun1(a):
        return a*3
    
    def foo():
        r = some_function()
        if r==(-1):
            return (-1)
        else:
           # do something
            return r
    print fun1('aa')
    
    
    def fun1(a):
        return a-2
    
    def foo(a):
        r = fun1(a)
        if r==(-1):
            return (-1)
        else:
           # do something
            return r
    print fun1(1)
    print '--------------'
    print foo(30)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    -1
    --------------
    28
    
    try:
    
    让我们用一个例子来看看try的机制:
    
    
    ------------------------------------------------
    try:
        print 'try...'
        r = 10 / 0
        print 'result:', r
    except ZeroDivisionError, e:
        print 'except:', e
    finally:
        print 'finally...'
    print 'END'
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    try...
    except: integer division or modulo by zero
    finally...
    END
    
    
    try:
        print 'try...'
        r = 10 / 5
        print 'result:', r
    except ZeroDivisionError, e:
        print 'except:', e
    finally:
        print 'finally...'
    print 'END'
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    try...
    result: 2
    finally...
    END
    
    
    def foo(s):
        return 10 / int(s)
    
    def bar(s):
        return foo(0) * 2
    
    def main():
        try:
            bar('0')
        except StandardError, xx:
            print 'Error!'
        finally:
            print 'finally...'
    
    main()
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    Error!
    finally
    
    调用堆栈:
    
    # err.py:
    def foo(s):
        return 10 / int(s)
    
    def bar(s):
        return foo(s) * 2
    
    def main():
        bar('0')
    
    main()
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 11, in <module>
        main()
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 9, in main
        bar('0')
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 6, in bar
        return foo(s) * 2
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 3, in foo
        return 10 / int(s)
    ZeroDivisionError: integer division or modulo by zero
    
    Process finished with exit code 1
    
    ------------------------------------------------------------------------
    # err.py:
    def foo(s):
        return 10 / int(s)
    
    def bar(s):
        return foo(s) * 2
    
    def main():
        bar('0')
    
    main()
    print 'xxxxxxxxxxxxxx'
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 11, in <module>
        main()
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 9, in main
        bar('0')
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 6, in bar
        return foo(s) * 2
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 3, in foo
        return 10 / int(s)
    ZeroDivisionError: integer division or modulo by zero
    
    Process finished with exit code 1
    
    
    
    
    # err.py:
    import logging
    def foo(s):
        return 10 / int(s)
    
    def bar(s):
        return foo(s) * 2
    
    def main():
        try:
            bar('0')
        except StandardError, e:
            logging.exception(e)
    
    main()
    print 'xxxxxxxxxxxxxx'
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a3.py
    xxxxxxxxxxxxxx
    ERROR:root:integer division or modulo by zero
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 11, in main
        bar('0')
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 7, in bar
        return foo(s) * 2
      File "C:/Users/TLCB/PycharmProjects/untitled/a3.py", line 4, in foo
        return 10 / int(s)
    ZeroDivisionError: integer division or modulo by zero
    
    Process finished with exit code 0
    
    
    抛出错误:
    
    因为错误是class,捕获一个错误就是捕获到该class的一个实例。
    
    
    因此,错误并不是凭空产生的,而是有意创建并抛出的。
    
    
    如果要抛出错误,首先根据需要,定义一个错误的class,选择好继承关系,
    
    然后,用raise语句抛出一个错误的实例:
    
    # err.py
    class FooError(StandardError):
        def __init__(self):
    
    def foo(self,s):
        n = int(s)
        if n==0:
            raise FooError('invalid value: %s' % s)
        return 10 / n
    
    -----------------------------------------------------------
    # err.py
    class FooError(StandardError):
        def __init__(self):
            print 11
        def foo(self, s):
            n = int(s)
            if n == 0:
                raise FooError('invalid value: %s' % s)
            return 10 / n
    
    
    from   mycompany.web.FooError import *
    s=FooError()
    print s.foo(5)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    11
    2
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    11
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a2.py", line 3, in <module>
        print s.foo(0)
      File "C:UsersTLCBPycharmProjectsuntitledmycompanywebFooError.py", line 8, in foo
        raise FooError('invalid value: %s' % s)
    TypeError: __init__() takes exactly 1 argument (2 given)
    
    最后,我们来看另一种错误处理的方式;
    
    def foo(s):
        n = int(s)
        return 10 / n
    
    def bar(s):
        try:
            return foo(s) * 2
        except StandardError, e:
            print 'Error!'
            raise
    
    def main():
        bar('0')
    
    main()
    
    def foo(s):
        n = int(s)
        return 10 / n
    
    def bar(s):
        try:
            return foo(s) * 2
        except StandardError, e:
            print 'Error!'
            raise
    
    def main():
        bar('0')
    
    main()
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a4.py
    Error!
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a4.py", line 15, in <module>
        main()
      File "C:/Users/TLCB/PycharmProjects/untitled/a4.py", line 13, in main
        bar('0')
      File "C:/Users/TLCB/PycharmProjects/untitled/a4.py", line 7, in bar
        return foo(s) * 2
      File "C:/Users/TLCB/PycharmProjects/untitled/a4.py", line 3, in foo
        return 10 / n
    ZeroDivisionError: integer division or modulo by zero

  • 相关阅读:
    unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8
    Unity SteamVR插件集成
    Unity3D Layer要点
    Unity利用Sapi进行windows语音开发
    Scratch入门课程(1)——把工具准备好
    【blockly教程】Blockly编程案例
    【blockly教程】第六章 Blockly的进阶
    【blockly教程】第五章 循环结构
    【blockly教程】第三章Blockly顺序程序设计
    【blockly教程】第四章 Blockly之选择结构
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349597.html
Copyright © 2011-2022 走看看