如何在 Python 中使用“raise”关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13957829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use "raise" keyword in Python
提问by Capurnicus
I have read the official definition of "raise", but I still don't quite understand what it does.
我已经阅读了“raise”的官方定义,但我仍然不太明白它的作用。
In simplest terms, what is "raise"?
简单来说,什么是“加注”?
Example usage would help.
示例用法会有所帮助。
采纳答案by Ignacio Vazquez-Abrams
It has 2 purposes.
它有两个目的。
yentup has given the first one.
It's used for raising your own errors.
if something: raise Exception('My error!')
它用于引发您自己的错误。
if something: raise Exception('My error!')
The second is to reraise the currentexception in an exception handler, so that it can be handled further up the call stack.
第二个是在异常处理程序中重新引发当前异常,以便它可以在调用堆栈上进一步处理。
try:
generate_exception()
except SomeException as e:
if not can_handle(e):
raise
handle_exception(e)
回答by Hymancogdill
回答by Martijn Pieters
raisecauses an exception to be raised. Some other languages use the verb 'throw' instead.
raise导致引发异常。其他一些语言使用动词“throw”代替。
It's intended to signal an error situation; it flags that the situation is exceptional to the normal flow.
它旨在表示错误情况;它标志着这种情况对于正常流程来说是异常的。
Raised exceptions can be caught again by code 'upstream' (a surrounding block, or a function earlier on the stack) to handle it, using a try, exceptcombination.
引发的异常可以由代码“上游”(周围的块或堆栈中较早的函数)再次捕获,以使用try,except组合进行处理。
回答by sampson-chen
You can use it to raise errors as part of error-checking:
您可以使用它来引发错误作为错误检查的一部分:
if (a < b):
raise ValueError()
Or handle some errors, and then pass them on as part of error-handling:
或者处理一些错误,然后将它们作为错误处理的一部分传递:
try:
f = open('file.txt', 'r')
except IOError:
# do some processing here
# and then pass the error on
raise
回答by Sohail Si
raisewithout any arguments is a special use of python syntax. It means get the exception and re-raise it. If this usage it could have been called reraise.
raise不带任何参数是 python 语法的特殊用法。这意味着获取异常并重新提出它。如果这种用法它可以被称为reraise.
raise
From The Python Language Reference:
来自Python 语言参考:
If no expressions are present, raise re-raises the last exception that was active in the current scope.
如果不存在表达式, raise 重新引发当前作用域中活动的最后一个异常。
If raiseis used alone without any argument is strictly used for reraise-ing. If done in the situation that is not at a reraise of another exception, the following error is shown:
RuntimeError: No active exception to reraise
如果raise单独使用没有任何参数严格用于reraise-ing。如果在没有重新引发另一个异常的情况下完成,则会显示以下错误:
RuntimeError: No active exception to reraise
回答by Stan Prokop
Besides raise Exception("message")and raisePython 3 introduced a new form, raise Exception("message") from e. It's called exception chaining, it allows you to preserve the original exception (the root cause) with its traceback.
此外raise Exception("message"),raisePython 3 引入了一种新形式,raise Exception("message") from e. 它被称为异常链接,它允许您通过回溯保留原始异常(根本原因)。
It's very similar to inner exceptions from C#.
它与 C# 的内部异常非常相似。
More info: https://www.python.org/dev/peps/pep-3134/

