如何使用“筹款" Python中的关键字 [英] How to use "raise" keyword in Python

查看:100
本文介绍了如何使用“筹款" Python中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了"raise"的正式定义,但我仍然不太了解它的作用.

I have read the official definition of "raise", but I still don't quite understand what it does.

最简单的说,筹款"是什么?

In simplest terms, what is "raise"?

示例用法会有所帮助.

推荐答案

它有2个用途.

yentup给出了第一个.

它用于引发您自己的错误.

It's used for raising your own errors.

if something:
    raise Exception('My error!')

第二个是在异常处理程序中引发 current 异常,以便可以在调用堆栈中进一步处理该异常.

The second is to reraise the current exception 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)

这篇关于如何使用“筹款" Python中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆