Python中的异常类型

news/2024/7/4 1:09:12 标签: python, file, object, list, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">
class="post-content">

class="title" title="Python中的异常类型 ">

class="content">

1、NameError:尝试访问一个未申明的变量
>>> v
NameError: name 'v' is not defined

2、ZeroDivisionError:除数为0
>>> v = 1/0
ZeroDivisionError: int division or modulo by zero

3、SyntaxError:语法错误
>>> int int
SyntaxError: invalid syntax (<pyshell#14>, line 1)

4、IndexError:索引超出范围
>>> List = [2]
>>> List[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
List[3]
IndexError: class="tags" href="/tags/LIST.html" title=list>list index out of range

5、KeyError:字典关键字不存在
>>> Dic = {'1':'yes', '2':'no'}
>>> Dic['3']
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
Dic['3']
KeyError: '3'

6、IOError:输入输出错误
>>> f = open('abc')
IOError: [Errno 2] No such class="tags" href="/tags/FILE.html" title=file>file or directory: 'abc'

7、AttributeError:访问未知对象属性
>>> class Worker:
def Work():
print("I am working")

>>> w = Worker()
>>> w.a
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
w.a
AttributeError: 'Worker' class="tags" href="/tags/OBJECT.html" title=object>object has no attribute 'a'

8、ValueError:数值错误
>>> int('d')

Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
int('d')
ValueError: invalid literal for int() with base 10: 'd'

9、TypeError:类型错误
>>> iStr = '22'
>>> iVal = 22
>>> obj = iStr + iVal;
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
obj = iStr + iVal;
TypeError: Can't convert 'int' class="tags" href="/tags/OBJECT.html" title=object>object to str implicitly

10、AssertionError:断言错误
>>> assert 1 != 1
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
assert 1 != 1
AssertionError



http://www.niftyadmin.cn/n/1522959.html

相关文章

html 注释_重新理解前端HTML篇DTD到底是什么?

大家好我是落尘&#xff0c;一名新进晋级的菜鸟小编&#xff0c;也是一名前端开发攻城狮&#xff0c;从今天起带大家重新理解前端&#xff0c;重新学习前端&#xff0c;让大家有新的理解&#xff0c;每天都会更新些前端知识&#xff0c;如果小主们觉得还可以那就关注一下吧&…

python 中的下划线

Python 用下划线作为变量前缀和后缀指定特殊变量。_xxx 不能用from module import *导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格&#xff1a;避免用下划线作为变量名的开始。因为下划线对解释器有特殊的意义&#xff0c;而且是内建标识符所使用的符号&…

python发送qq邮件_Python3实现发送QQ邮件功能(html)

本文为大家分享了Python3实现发送QQ邮件功能&#xff1a;html&#xff0c;供大家参考&#xff0c;具体内容如下 之前已经成功发送了qq邮件。下面贴出html格式的qq邮件 import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender xxx…

python的程序基本风格

注释&#xff1a;简洁明了的说明对于程序的维护还是比较重要的 文档&#xff1a;通过__doc__特别变量&#xff0c;动态获得文档字符串&#xff0c;在模块、类声明、函数声明中第一个没有赋值的字符串可以用属性obj.__doc__来访问&#xff0c;obj为模块、类、函数的名字。 缩进&…

centos7.0环境mysql8.0源码安装_Centos8搭建Java环境(JDK1.8+Nginx+Tomcat9+Redis+Mysql)

一、开篇1.1 目的每次换新的服务器&#xff0c;都要找资料配下环境&#xff0c;所以我写这篇文章&#xff0c;重新梳理了一下&#xff0c;方便了自己,希望也能给大家带来一些帮助。安装的软件有&#xff1a;JDK1.8 Nginx Tomcat9 Redis Mysql1.2 服务器简介昨天刚购买的1核…

揭开正则表达式的神秘面纱

引言 正则表达式&#xff08;regular expression&#xff09;就是用一个“字符串”来描述一个特征&#xff0c;然后去验证另一个“字符串”是否符合这个特征。比如 表达式“ab” 描述的特征是“一个 a 和 任意个 b ”&#xff0c;那么 ab, abb, abbbbbbbbbb 都符合这个特征。 正…

zookeeper安装_9、Zookeeper安装教程

&#xfeff;[TOC]1、环境准备1.1下载zooKeeper查阅hadoop2.7.3的文档我们可以看到hadoop2.7.3在搭建高可用的时候使用的是zookeeper-3.4.2版本&#xff0c;所以我们也按照hadoop官网的提示&#xff0c;接下来我们安装zookeeper-3.4.2版本.进入官网下载ZooKeeper3.4.2版本   …

python导入模块介绍_Python模块导入的相关介绍

浅谈python模块的导入操作 1.什么是模块 在Python中有一个概念叫做模块(module)。所谓模块&#xff0c;就是将代码量较大的程序分割成多个有组织的&#xff0c;彼此独立但双能互相交互的代码片段&#xff0c;这些自我包含的有组织的代码段就是模块。 2.模块的特点 python中的模…