Py学习  »  Python

python:从函数返回异常

Subbeh • 5 年前 • 1301 次点击  

假设我有以下功能:

def test():
  ...
  if x['error']:
    raise

这将引发一个异常,无论 x['error'] 是否已定义。

相反,如果我尝试这个,它不会抛出任何异常:

def test():
  ...
  try:
    if x['error']:
      raise
  except:
    return

如果定义了一个特定的值,我如何测试它并返回一个异常;如果没有定义,我如何成功返回?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38697
 
1301 次点击  
文章 [ 3 ]  |  最新文章 5 年前