在python中异常的分析

高洛峰
发布: 2017-03-08 09:49:15
原创
1719人浏览过

每个异常都是一 些类的实例,这些实例可以被引发,并且可以用很多种方法进行捕捉,使得程序可以捉住错误并且对其进行处理

>>> 1/0

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    1/0
ZeroDivisionError: integer division or modulo by zero
登录后复制

异常处理

捕捉异常可以使用try/except语句。

>>> def inputnum():
    x=input('Enter the first number: ')
    y=input('Enter the first number: ')
    try:
        print x/y
    except ZeroDivisionError:
        print "The second number can't be zero"

        
>>> inputnum()
Enter the first number: 10
Enter the first number: 0
The second number can't be zero
登录后复制

raise 触发异常

>>> class Muff:
    muffled=False
    def calc(self,expr):
        try:
            return eval(expr)
        except ZeroDivisionError:
            if self.muffled:
                print 'Division by zero is illegal'
            else:
                raise

            
>>> c=Muff()
>>> c.calc(10/2)

Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    c.calc(10/2)
  File "<pyshell#31>", line 5, in calc
    return eval(expr)
TypeError: eval() arg 1 must be a string or code object
>>> c.calc('10/2')
>>> c.calc('1/0')

Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    c.calc('1/0')
  File "<pyshell#31>", line 5, in calc
    return eval(expr)
  File "<string>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> c.muffled=True
>>> c.calc('1/0')
Division by zero is illegal
登录后复制

多种异常类型

立即学习Python免费学习笔记(深入)”;

百度文心百中
百度文心百中

百度大模型语义搜索体验中心

百度文心百中 22
查看详情 百度文心百中
try:
    x=input('Enter the first number:')
    y=input('Enter the seconed number:')
    print x/y
except ZeroDivisionError:
    print "The second number can't be zero!"
except TypeError:
    print "That wasn't a number,was it?"
登录后复制

同时 捕捉多个异常

try:
    x=input('Enter the first number:')
    y=input('Enter the seconed number:')
    print x/y
except(ZeroDivisionError,TypeError,NameError):
    print 'Your numbers were bogus...'
登录后复制

 捕捉对象

try:
    x=input('Enter the first number:')
    y=input('Enter the seconed number:')
    print x/y
except(ZeroDivisionError,TypeError),e:
    print e

    
Enter the first number:1
Enter the seconed number:0
integer division or modulo by zero
登录后复制

 捕捉所有异常

try:
    x=input('Enter the first number:')
    y=input('Enter the seconed number:')
    print x/y
except:
    print 'something wrong happened...'

    
Enter the first number:
something wrong happened...
登录后复制

更多在python中异常的分析相关文章请关注PHP中文网!

相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号