Python 抛自定义异常
2021/7/29 1:06:00
本文主要是介绍Python 抛自定义异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
自定义异常,抛自定义异常,捕获异常,打印异常堆栈
import traceback class UserNameTooLong(Exception): def __init__(self, long): self.long = long def __str__(self): return 'user name length is ' + str(self.long) + " too long" def raise_error(): s = input("input your name") raise UserNameTooLong(len(s)) def call_raise_error(): raise_error() def input_value(): return None def errorAppear(): try: s = input_value() if s is None: print("s is none") print(len(s)) except TypeError: print("catch exception") traceback.print_exc() print(traceback.format_exc()) # errorAppear() call_raise_error()
这篇关于Python 抛自定义异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南