python笔记2-错误类型Error Handling

    技术2022-07-10  95

    区分Error handling和Debugging

    前者指代码放入运行环境后出现的无法预测的错误,如权限问题、数据库改变、服务(server)关闭

    后者指代码本身的错误

     

    error types:syntax errors 语法错误、runtime errors(如ZeroDivisionError)、logic errors(单元测试和驱动测试开发可以自动找出逻辑错误,可以看看python里的unit test;堆栈追踪)

    runtime error:

    比如当服务器关闭、用户输入错误等无法预测的情况出现导致代码无法运行,就可以使用try/except/finally。

    x=42 y=0 try: print(x/y) except ZeroDivisionError as e: print('not allowed to divid by zero') else: print('something else wrong') finally: print('this is our cleanup code')

    输出如下

    not allowed to divid by zero this is our cleanup code

     

    Processed: 0.009, SQL: 9