Heim  >  Artikel  >  Backend-Entwicklung  >  Ausnahmeanalyse in Python

Ausnahmeanalyse in Python

高洛峰
高洛峰Original
2017-03-08 09:49:151479Durchsuche

Jede Ausnahme ist eine Instanz einer Klasse. Diese Instanzen können auf viele Arten ausgelöst und abgefangen werden, sodass das Programm Fehler abfangen und behandeln kann

>>> 1/0

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

Ausnahmebehandlung

Um Ausnahmen abzufangen, können try/exclusive-Anweisungen verwendet werden.

>>> 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

Trigger-Ausnahme auslösen

>>> 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 division or modulo by zero
>>> c.muffled=True
>>> c.calc(&#39;1/0&#39;)
Division by zero is illegal

Mehrere Ausnahmetypen

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?"

Mehrere Ausnahmen gleichzeitig abfangen

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;

Erfassen Objekte

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 division or modulo by zero

Alle Ausnahmen erfassen

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...

Weitere Artikel zur Ausnahmeanalyse in Python finden Sie auf der chinesischen PHP-Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn