A detailed description of the exception in Python

Source: Internet
Author: User
Each exception is an instance of some class that can be thrown and can be captured in a number of ways so that the program can catch the error and process it


>>> 1/0traceback (most recent):  File "<pyshell#0>", line 1, in <module>    1/ 0zeropisionerror:integer pision or modulo by zero


Exception handling

You can use the Try/except statement to catch an exception.

>>> 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:10enter the first number:0the second number can ' t be zero


Raise Trigger exception

>>> class Muff:muffled=false def calc (self,expr): try: return eval (expr) except Zerodivisionerror:if self.muffled:print ' division by ze Ro is illegal ' else:raise >>> c=muff () >>> C.calc (10/2) Traceback (M OST recent call last): File "<pyshell#33>", line 1, in <module> c.calc (10/2) file "<pyshell#31>", L Ine 5, in Calc return eval (expr) typeerror:eval () Arg 1 must is a string or code object>>> c.calc (' 10/2 ') >& Gt;> 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, and Calc return eval (expr) file "<string>", line 1, in <module>zer Odivisionerror:integer pision or modulo by zero>>> c.muffled=true>>> c.calc (' 1/0 ') Division by zero is I Llegal 


Multiple exception types

Try:    x=input (' Enter the first number: ')    y=input (' Enter the seconed number: ')    print x/yexcept Zerodivisionerror:    print "The second number can ' t be zero!" Except TypeError:    print "That wasn ' t a number,was it?"

Capturing multiple exceptions at the same time

Try:    x=input (' Enter the first number: ')    y=input (' Enter the seconed number: ')    print x/yexcept ( Zerodivisionerror,typeerror,nameerror):    print ' Your numbers were bogus ... '

Snapping objects

Try:    x=input (' Enter the first number: ')    y=input (' Enter the seconed number: ')    print x/yexcept ( Zerodivisionerror,typeerror), E:    print e    Enter The first number:1enter the seconed Number:0integer pision or Modulo by zero


Catch all exceptions

try:x=input (' Enter the first number: ') y=input (' Enter the seconed number: ')    Print X/yexcept:print ' Something wrong happened ... ' Enter The first number:something wrong happened ... 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.