首页 > 编程语言 >python中常见的异常 error

python中常见的异常 error

时间:2024-02-22 11:35:51浏览次数:32  
标签:Traceback last python 常见 call File error line

python中常见的异常

在python2中可以通过一个模块来查看所有的内置异常,而在python3中就无法查看。

>>> importexceptions>>>dir(exceptions)

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__doc__', '__name__', '__package__']

下面介绍介几个使用的比较频繁的异常。

1.NameError:尝试访问一个未申明的变量。

>>>foo

Traceback (most recent call last):

File"", line 1, in NameError: name'foo' is not defined

NameError表示我们访问了一个没有初始化的变量。

任何可访问的变量必须在名称空间里列出,访问变量需要由解释器进行搜索,如果请求的名字没有在任何名称空间里找到,那么就会生成一个NameError异常。

2.ZeroDivisionError:除数为0.

>>> 1/0

Traceback (most recent call last):

File"", line 1, in ZeroDivisionError: division by zero

任何一个数值被零除都会导致ZeroDivisionError异常。

3.SyntaxError:python解释器语法错误。

>>> for i inli:

...printi

File"", line 2

printi^SyntaxError: Missing parenthesesin call to 'print'. Did you mean print(i)?

SyntaxError异常是唯一不是在运行时发生的异常。

它代表python代码中有一个不正确的结构,在他改正之前程序无法运行。

这些错误一般都是在编译时发生的,python解释器无法把你的脚本转化成python字节代码。

4.IndexError:请求的索引超出范围。

>>>li

[1, 2, 3, 4]>>> li[10]

Traceback (most recent call last):

File"", line 1, in IndexError: list index out of range

5.KeyError:请求一个不存在的字典关键字。

>>> dict1 = {"name":'kebi',"sex":'boy'}>>> dict1['age']

Traceback (most recent call last):

File"", line 1, in KeyError:'age'

6.IOError/FileNotFoundError:输入/输出错误

类似尝试打开一个不存在的磁盘文件一类的操作会引发一个操作系统输入/输出错误。

在python2中引发IOError异常;在python3中引发/FileNotFoundError异常。

#在python2中

>>> f = open('helo')

Traceback (most recent call last):

File"", line 1, in IOError: [Errno2] No such file or directory: 'helo'

#在python3中

>>> f = open('blah')

Traceback (most recent call last):

File"", line 1, in FileNotFoundError: [Errno2] No such file or directory: 'blah'

7.AttributeError:尝试访问未知的对象属性。

>>> classmyClass(object):

...pass...>>> myInst =myClass()>>>myInst.foo

Traceback (most recent call last):

File"", line 1, in AttributeError:'myClass' object has no attribute 'foo'

标签:Traceback,last,python,常见,call,File,error,line
From: https://www.cnblogs.com/97zs/p/18026950

相关文章

  • Python Web 开源框架排行榜
    截止到2024年2月22日,对GithubStar>900的PythonWeb开源框架,按照Star数量,从高到低排序,具体排名如下:1.DjangoDjangoisahigh-levelPythonwebframeworkthatencouragesrapiddevelopmentandclean,pragmaticdesign.About: TheWebframeworkforperfec......
  • python实现zip分卷压缩与解压
    1. python实现zip分卷压缩WinHex开始16进制一个一个文件对比WinRar创建的分卷压缩和单个zip文件的差异。如果想把单个大文件 test.zip ->分卷文件 test.z01、test.z02、test.zip首先,在创建的第一个分卷文件 test.z01的前面加上 \x50\x4b\x07\x08 这个是分卷压缩......
  • python在B站爬糖豆广场舞
    先附上代码:importrequests,refromlxmlimportetree#这是单页面下载,翻页的目前还不会url='https://search.bilibili.com/all?vt=96737335&keyword=%E7%B3%96%E8%B1%86%E5%B9%BF%E5%9C%BA%E8%88%9E'headers={'user-agent':'Mozilla/5.0(Window......
  • 1.3 使用pip管理Python扩展库
    常用pip命令使用方法pip命令示例说明pipfreeze[>packages.txt]列出已安装模块及其版本号,可使用重定向符>把扩展库信息保存到文件packages.txt中pipinstallSomePackage[==version]在线安装SomePackage模块,可以使用方括号内的形式指定扩展库版本pipinstallSo......
  • 1.2 Python安装与简单使用
    Python3.6.8安装Python官网:https://www.python.org/Python3.6.8官网:https://www.python.org/downloads/release/python-368/按照提示安装即可,安装完成后,按win+R打开命令行,输入python-V,出现版本号,说明安装成功在开始菜单中选择IDLE(PythonGUI)即可启动Python解释器......
  • 1.1 如何选择Python版本
    简介Python是一门解释型高级语言,支持伪编译可以把Python源程序转换为字节码来优化程序和提高运行速度,支持使用py2exe、py2app、cx_Freeze或pyinstaller工具将Python程序打包为不同平台上的可执行程序,可以在没有安装Python解释器和相关依赖包的系统中运行Python支持多版本并存......
  • Python练习案例_Pico Fermi Bagels猜数字游戏
    案例介绍--《Python编程快速上手2》在PicoFermiBagels这个逻辑推理游戏中,你要根据线索猜出一个三位数。游戏会根据你的猜测给出以下提示之一:如果你猜对一位数字但数字位置不对,则会提示“Pico”;如果你同时猜对了一位数字及其位置,则会提示“Fermi”;如果你猜测的数字及其位置......
  • Python+Faker+Pandas数据库造数
    今日分享一些Python常用的东西,整理一些小笔记,比如Faker的使用,panda的使用1、使用faker造数据简介测试工作中,经常会遇到需要制造大量测试数据的时候,如果手动造数据必然会浪费大量时间Faker是一个制造数据的强大的python库,可以制造姓名、电话、身份证、地址、邮箱等等各种各样伪......
  • Go 100 mistakes - #50: Checking an error type inaccurately
       ......
  • Python中logging模块
     在项目中我们常常需要打印日志,特别是在系统级项目上一般都会有自己日志模块,下面我们将介绍下Python中自带的logging模块(注意这是模块的名称并不是类)一、基本使用logging是一个包的名称,我们真正使用的是logging.Logger这个类。但是我们不能使用常规的方式进行初始化,......