zoukankan      html  css  js  c++  java
  • 20191209---自定义异常类--转载

    Python:用户自定义异常:https://www.cnblogs.com/ywxbbbbb/p/9958613.html

    实际开发中,有时候系统提供的异常类型不能满足开发的需求。这时候你可以通过创建一个新的异常类来拥有自己的异常。异常类继承自 Exception 类,可以直接继承,或者间接继承。

    1.自定义异常类型 

    复制代码
    #1.用户自定义异常类型,只要该类继承了Exception类即可,至于类的主题内容用户自定义,可参考官方异常类
    class TooLongExceptin(Exception):
        "this is user's Exception for check the length of name "
        def __init__(self,leng):
            self.leng = leng
        def __str__(self):
            print("姓名长度是"+str(self.leng)+",超过长度了")
    复制代码

    2.如何手动抛出异常:raise

    系统的自带的异常只要触发会自动抛出,比如NameError,但用户自定义的异常需要用户自己决定什么时候抛出。
    raise 唯一的一个参数指定了要被抛出的异常。它必须是一个异常的实例或者是异常的类(也就是 Exception 的子类)。大多数的异常的名字都以"Error"结尾,所以实际命名时尽量跟标准的异常命名一样。

    复制代码
    #1.用户自定义异常类型
    class TooLongExceptin(Exception):
        "this is user's Exception for check the length of name "
        def __init__(self,leng):
            self.leng = leng
        def __str__(self):
            print("姓名长度是"+str(self.leng)+",超过长度了")
     
    #2.手动抛出用户自定义类型异常
    def name_Test():
            name = input("enter your naem:")
            if len(name)>4:
                raise TooLongExceptin(len(name))  #抛出异常很简单,使用raise即可,但是没有处理,即捕捉
            else :
                print(name)
     
    #调用函数,执行
    name_Test()
    -----------------执行时满足条件后抛出一个用户定义的异常如下:--------------------------------------
    enter your naem:是打发斯蒂芬
    Traceback (most recent call last):
    姓名长度是6,超过长度了
      File "D:/pythoyworkspace/file_demo/Class_Demo/extion_demo.py", line 21, in <module>
        name_Test()
    __main__.TooLongExceptin: <exception str() failed>
    复制代码

    3.捕捉用户手动抛出的异常

    复制代码
    #1.捕捉用户手动抛出的异常,跟捕捉系统异常方式一样
    def name_Test():
        try:
            name = input("enter your naem:")
            if len(name)>4:
                raise TooLongExceptin(len(name))
            else :
                print(name)
     
        except TooLongExceptin,e_result:  #这里异常类型是用户自定义的
            print("捕捉到异常了")
            print("打印异常信息:",e_result)
     
    #调用函数,执行
    name_Test()
    ==========执行结果如下:==================================================
    enter your naem:aaafsdf
    捕捉到异常了
    Traceback (most recent call last):
    打印异常信息: 姓名长度是7,超过长度了
    姓名长度是7,超过长度了
      File "D:/pythoyworkspace/file_demo/Class_Demo/extion_demo.py", line 16, in name_Test
        raise TooLongExceptin(len(name))
    __main__.TooLongExceptin: <exception str() failed>
     
    During handling of the above exception, another exception occurred:
     
    Traceback (most recent call last):
      File "D:/pythoyworkspace/file_demo/Class_Demo/extion_demo.py", line 26, in <module>
        name_Test()
      File "D:/pythoyworkspace/file_demo/Class_Demo/extion_demo.py", line 22, in name_Test
        print("打印异常信息:",e_result)
    TypeError: __str__ returned non-string (type NoneType)
  • 相关阅读:
    Redis集群~StackExchange.redis连接Twemproxy代理服务器
    开源的Android视频播放器
    Servlet 实现文件的上传与下载
    HDU1878 欧拉回路
    C#根据域名查询IP(CMD命令参数输入或者启动程序后再输入查询)
    Windows API获取系统配置文件的配置参数
    Lucene核心--构建Lucene搜索(下篇,理论篇)
    Lucene核心--构建Lucene搜索(上篇,理论篇)
    hdu1397(素数组和成偶数的个数 用 标记法)
    hdu1248
  • 原文地址:https://www.cnblogs.com/lnn123/p/12013042.html
Copyright © 2011-2022 走看看