python中的错误处理

WBOY
Release: 2016-06-10 15:05:24
Original
1216 people have browsed it

用错误码来表示是否出错十分不便,因为函数本身应该返回的正常结果和错误码混在一起,造成调用者必须用大量的代码来判断是否出错:

def foo():
  r = some_function()
  if r==(-1):
    return (-1)
  # do something
  return r

def bar():
  r = foo()
  if r==(-1):
    print 'Error'
  else:
    pass

Copy after login

但是Go就是这么干的,哈哈!

python 中还是用try … except….finally这种方式来处理的。

try:
  print 'try...'
  r = 10 / 0
  print 'result:', r
except ZeroDivisionError, e:
  print 'except:', e
finally:
  print 'finally...'
print 'END'
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template