Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 1

pish7/stdpython

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
stdpython
/
python.lang
/
exception.01.py
stdpython
/
python.lang
/
exception.01.py
exception.01.py 2.91 KB
Copy Edit Raw Blame History
pish7 authored 2025年12月28日 01:28 +08:00 . python
# 使用 except 捕获所有的异常很危险,因为这不仅会隐藏你有心理准备的错误,还会隐藏你没有考虑过的错误。
# 这还将捕获用户使用 Ctrl+C 终止执行的企图、调用函数 sys.exit 来终止执行的企图等。
# 在大多数情况下,更好的选择是使用 except Exception as e 并对异常对象进行检查。
# 这样做将让不是从 Exception 派生而来的为数不多的异常成为漏网之鱼,
# 其中包括 SystemExit 和 KeyboardInterrupt,因为它们是从 BaseException(Exception的超类)派生而来的。
# 使用 raise 来抛出异常,后面可跟一个异常类(这时将自动创建一个实例)或一个异常实例
# raise Exception
# raise Exception("我是异常信息")
# raise #再次抛出异常(用于except语句中)
# 创建异常类,必须直接或间接地继承自 Exception
class MyException1(Exception):
pass
class MyException2(Exception):
def __init__(self, msg="no msg"):
self.msg = msg
try:
val = int(input("请输入数值:"))
if val == 0:
# 使用 raise 来抛出异常
raise MyException2("zero")
elif val == 1:
raise MyException1(val)
# 可以抛出异常对象,也可以抛出异常类
raise Exception
except MyException1 as e:
# [as e]是可选的,用于获取异常对象
print("get MyException", e)
raise # 再次抛出异常
# 使用元组来捕获多种异常 (ZeroDivisionError, TypeError, NameError) as e
except (ZeroDivisionError, TypeError, NameError) as e:
print("使用一个元组来捕获多种异常", e)
except Exception as e:
# 注意,这个 except 并不能捕获所有异常,因为 Exception 上头还有基类 BaseException
try:
# 这里直接访问 e 的 msg 属性,不管 e 中是否有 msg 属性,有则大吉,无则在 except 中处理
print("get it:", e.msg) # ty:ignore[unresolved-attribute]
except:
# 抛出其它异常,那么前面捕获到的异常也会存在于"异常链"中,即异常上下文中
# 完整的写法是 `raise ValueError from e`
raise ValueError
except:
# 可捕获任意异常
print("all except")
# 重新抛出异常时会自动传递异常上下文
# 可以使用 from 来指定异常上下文,包括 None 值来指定没有上下文
raise ValueError from None
else:
# 没有发生异常,注意,不是"没有捕获到异常"
print("很好,没有异常发生 ")
finally:
# 即使是 raise 或 return 也不能阻止 finally 子句的运行
print("总是执行")
# 可选择是否抑制异常的"计算器"类
class MuffledCalculator:
muffled = False # 是否抑制异常
def calc(self, expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print("Division by zero is illegal")
else:
raise
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

python 语法学习,demo 小示例
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/pish7/stdpython.git
git@gitee.com:pish7/stdpython.git
pish7
stdpython
stdpython
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /