关于python中异常的详细说明

高洛峰
Libérer: 2017-03-15 13:57:56
original
1478 Les gens l'ont consulté

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


>>> 1/0

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    1/0
ZeropisionError: integer pision or modulo by zero
Copier après la connexion


异常处理

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

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

        
>>> inputnum()
Enter the first number: 10
Enter the first number: 0
The second number can&#39;t be zero
Copier après la connexion


raise 触发异常

>>> class Muff:
    muffled=False
    def calc(self,expr):
        try:
            return eval(expr)
        except ZeroDivisionError:
            if self.muffled:
                print &#39;Division by zero is illegal&#39;
            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(&#39;10/2&#39;)
>>> c.calc(&#39;1/0&#39;)

Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    c.calc(&#39;1/0&#39;)
  File "<pyshell#31>", line 5, in calc
    return eval(expr)
  File "<string>", line 1, in <module>
ZeroDivisionError: integer pision or modulo by zero
>>> c.muffled=True
>>> c.calc(&#39;1/0&#39;)
Division by zero is illegal
Copier après la connexion


多种异常类型

try:
    x=input(&#39;Enter the first number:&#39;)
    y=input(&#39;Enter the seconed number:&#39;)
    print x/y
except ZeroDivisionError:
    print "The second number can&#39;t be zero!"
except TypeError:
    print "That wasn&#39;t a number,was it?"
Copier après la connexion

同时 捕捉多个异常

try:
    x=input(&#39;Enter the first number:&#39;)
    y=input(&#39;Enter the seconed number:&#39;)
    print x/y
except(ZeroDivisionError,TypeError,NameError):
    print &#39;Your numbers were bogus...&#39;
Copier après la connexion

捕捉对象

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

    
Enter the first number:1
Enter the seconed number:0
integer pision or modulo by zero
Copier après la connexion


捕捉所有异常

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

    
Enter the first number:
something wrong happened...
Copier après la connexion


Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!