python调试和测试有什么区别和联系_python 错误、调试和测试

1 ‘‘‘

2 Created on 2018年7月28日3 @filename: error_info.py4 @author: liupf5 ‘‘‘

6 ‘‘‘

7 try:8 print(‘try...‘)9 r = 10 / 1#int(‘a‘)10 print(‘result:‘, r)11 except ZeroDivisionError as e:12 print(‘except:‘, e)13 except ValueError as e:14 print(‘ValueError:‘, e)15 else:16 print("no error")17 finally:18 print(‘finally...‘)19 print(‘END‘)20 ‘‘‘

21 #异常类的继承关系

22 #https://docs.python.org/3/library/exceptions.html#exception-hierarchy

23 ‘‘‘

24 BaseException25 +-- SystemExit26 +-- KeyboardInterrupt27 +-- GeneratorExit28 +-- Exception29 +-- StopIteration30 +-- StopAsyncIteration31 +-- ArithmeticError32 | +-- FloatingPointError33 | +-- OverflowError34 | +-- ZeroDivisionError35 +-- AssertionError36 +-- AttributeError37 +-- BufferError38 +-- EOFError39 +-- ImportError40 | +-- ModuleNotFoundError41 +-- LookupError42 | +-- IndexError43 | +-- KeyError44 +-- MemoryError45 +-- NameError46 | +-- UnboundLocalError47 +-- OSError48 | +-- BlockingIOError49 | +-- ChildProcessError50 | +-- ConnectionError51 | | +-- BrokenPipeError52 | | +-- ConnectionAbortedError53 | | +-- ConnectionRefusedError54 | | +-- ConnectionResetError55 | +-- FileExistsError56 | +-- FileNotFoundError57 | +-- InterruptedError58 | +-- IsADirectoryError59 | +-- NotADirectoryError60 | +-- PermissionError61 | +-- ProcessLookupError62 | +-- TimeoutError63 +-- ReferenceError64 +-- RuntimeError65 | +-- NotImplementedError66 | +-- RecursionError67 +-- SyntaxError68 | +-- IndentationError69 | +-- TabError70 +-- SystemError71 +-- TypeError72 +-- ValueError73 | +-- UnicodeError74 | +-- UnicodeDecodeError75 | +-- UnicodeEncodeError76 | +-- UnicodeTranslateError77 +-- Warning78 +-- DeprecationWarning79 +-- PendingDeprecationWarning80 +-- RuntimeWarning81 +-- SyntaxWarning82 +-- UserWarning83 +-- FutureWarning84 +-- ImportWarning85 +-- UnicodeWarning86 +-- BytesWarning87 +-- ResourceWarning88

89

90 import logging91

92 def foo(s):93 return 10 / int(s)94

95 def bar(s):96 return foo(s) * 297

98 def main():99 try:100 bar(‘0‘)101 except Exception as e:102 print(‘Error:‘, e)103 logging.exception(e)104 finally:105 print(‘finally...‘)106

107 main()108 print(‘END‘)109

110 class FooError(ValueError):111 pass112 def foo(s):113 n = int(s)114 if n==0:115 raise FooError(‘invalid value: %s‘ % s)116 return 10/n117

118 foo(‘0‘)119

120 def foo(s):121 n = int(s)122 if n==0:123 raise ValueError(‘invalid value:%s‘ % s)124 return 10/n125

126 def bar():127 try:128 foo(‘0‘)129 except ValueError as e:130 print(‘ValueError!‘)131 # raise132 raise ValueError(‘input error!‘)133 bar()134

135

136 #assert会抛出AssertionError:%s137 def foo(s):138 n = int(s)139 assert n != 0, ‘n is zero!‘140 return 10/n141

142 def main():143 foo(‘0‘)144

145 main()146 ‘‘‘

147

148 #python -0 err.py #可以关掉assert

149

150 ‘‘‘

151 import logging152 logging.basicConfig(level=logging.INFO)153 s = ‘0‘154 n = int(s)155 logging.info(‘n = %d‘ % n)156 print(10/n)157

158 import pdb159 s = ‘0‘160 n = int(s)161 pdb.set_trace()#插入一个断点,自动启动pdb162 print(10/n)163 ‘‘‘

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值