zoukankan      html  css  js  c++  java
  • python中异常的介绍

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

    >>> 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')
    5
    >>> 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

    多种异常类型

    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...
  • 相关阅读:
    https://jwt.io/一个可以解析token的神奇网站
    .net core 发布IIS 出现Http 500错误
    C# Session 操作类
    大话IdentityServer4之使用 IdentityServer4 保护 ASP.NET Core 应用
    c# FTP操作类
    将字符串写入记事本
    记录一下自己在MVC项目中如何防CSRF攻击,直接上代码
    Asp.Net Core 开发之旅之NLog日志
    Asp.Net Core 开发之旅之.net core 连接数据库
    css为图片添加一层蒙版并在上层显示文字等
  • 原文地址:https://www.cnblogs.com/nsds/p/6515216.html
Copyright © 2011-2022 走看看