# python自定义异常类

class BaseException(Exception):
'''异常类基类'''
pass

class WX_Exception(BaseException):
'''自定义的WX异常类'''
def __init__(self, msg):
self.msg = msg

def __str__(self):
return 'WX异常'

def demo():
'''使用类的一个例子'''
try:
# raise 人为抛出异常，使用WX_Exception进行捕获
raise WX_Exception("this is messages")
except WX_Exception as ex:
print(ex) # 输出的是__str__部分返回的内容
print(ex.msg) # 输出的是__init__中定义的msg

demo()


[En]

About the author: focus on the field of software automation testing. If you have any questions or suggestions, please let us know!

[En]

Copyright notice: the copyright of this article belongs to both the author and the blog Park. You are welcome to reprint it, but this notice must be retained without the author’s consent, and a link to the original text is given in a prominent position on the article page.

Original: https://www.cnblogs.com/yhleng/p/13445053.html
Author: _天枢
Title: python自定义异常类

(0)