zoukankan      html  css  js  c++  java
  • python学习笔记(8)异常处理

    try:
        pass  # 正常执行代码
    except Exception as e:
        pass  # 报错信息和异常处理机制
    
    try:
        with open("vue3.html") as f:
            data = f.read()
        print("正常打开")
    except Exception as e:  # Exception是所有异常
        print("e",e)
    
    # try:
    #     with open("vue3.html") as f:
    #         data = f.read()
    #     print("正常打开")
    #     d = {}
    #     d["a"]
    # except (FileNotFoundError, KeyError) as e:  # 文件没有找到异常
    #     print("e",e)
    
    try:
        with open("vue3.html") as f:
            data = f.read()
        print("正常打开")
        d = {}
        d["a"]
    except FileNotFoundError as e:  # 文件没有找到异常
        print("e",e)
    
    except KeyError as e:  # key错误异常
        print("e",e)
    
    finally:
        print("无论是否出现异常,一定会执行!")
    
    # 自己定义异常
    class TooLongException(Exception):
        def __init__(self, *args: object) -> None:
            self.len = len
        
        def __str__(self):
            return "输入姓名长度是" + str(self.len) + ",超过长度了"
    
    try:
        name = input("enter your name:")
        if len(name) > 5:
            raise TooLongException(len(name))
        else:
            print(name)
    
    except TooLongException as error:
        print("打印异常信息:", error)
  • 相关阅读:
    stl_hash_set.h
    stl_hash_map.h
    stl_algobase.h
    stl_relops.h
    stl_algo.h
    VC6常用插件
    visual assist(VA)设置快捷键(其它安装的插件设置快捷键也在这里)
    SVN常用命令说明
    Android 调用相册 拍照 实现系统控件缩放 切割图片
    Android 一个3D相册源码
  • 原文地址:https://www.cnblogs.com/tortoise512/p/15112297.html
Copyright © 2011-2022 走看看