自定义异常,抛自定义异常,捕获异常,打印异常堆栈

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()

 

相关文章:

  • 2021-10-09
  • 2021-08-21
  • 2021-05-14
  • 2022-12-23
  • 2021-07-13
  • 2022-02-10
猜你喜欢
  • 2022-01-02
  • 2021-09-03
  • 2022-02-24
  • 2022-02-07
  • 2022-12-23
  • 2022-02-16
  • 2021-12-13
相关资源
相似解决方案