Basic Python Learning--class and exception

 

class Cat:
    def __init__(self, name):
        print("__init__")
        self.gender = "male"
        self.age = 2
        self.color = "orange"
        self.name = name

    def __enter__(self):
        print("__enter__")
        return self

    def bug_cat1_broken_eye(self):
        raise Exception(10)

    def bug_cat2_broken_leg(self):
        raise Exception("broken leg")
        print("after raise Exception")

    def speak(self):
        print(self.name, end=" ")
        print("miao miao miao")

    def run(self):
        print(self.name, end=" ")
        print("quietly and fast")

    def how_old_am_i(self, age):
        if self.age == age:
            print("bingo")
            return
        for i in range(10):
            new_age = int(input("try again"))
            if new_age == self.age:
                print("bingo")
                return
        else:
            print("sorry")
            return

    def __exit__(self, exc_type, exc_val, exc_tb):

        print("__exit__")
        print("type:", end="")
        print(exc_type)
        print("val:", end="")
        print(exc_val)
        print("trace:", end="")
        print(exc_tb)

    def __del__(self):
        print("__del__")
        print(self.name, end=" ")
        print("was deleted")


def test_except():
    xixi = Cat("xixi")
    xixi.bug_cat1_broken_eye()
    print("after xixi.bug_cat1_broken_eye()")


def test_test_except():
    try:
        test_except()
    except Exception as e:
        print("test_test_except ", e)
        raise Exception(e)
    print("after test_except()")


try:
    test_test_except()
except Exception as e:
    print("except Exception bottom", e)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值