A detailed description of exceptions in python

Source: Internet
Author: User
This article introduces the exception details in python. Each exception is a class instance. these instances can be thrown and can be captured in many ways, this allows the program to catch and process errors.


>>> 1/0Traceback (most recent call last):  File "
 
  ", line 1, in 
  
       1/0ZeropisionError: integer pision or modulo by zero
  
 


Exception handling

You can use the try/try T statement to catch exceptions.

>>> 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 zero is illegal'            else:                raise            >>> c=Muff()>>> c.calc(10/2)Traceback (most recent call last):  File "
 
  ", line 1, in 
  
       c.calc(10/2)  File "
   
    ", 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 "
    
     ", line 1, in 
     
          c.calc('1/0')  File "
      
       ", line 5, in calc return eval(expr) File "
       
        ", line 1, in 
        
         ZeroDivisionError: integer pision or modulo by zero>>> c.muffled=True>>> c.calc('1/0')Division by zero is illegal
        
       
      
     
    
   
  
 


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

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

Object capturing

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

The above is a detailed description of exceptions in python. For more information, see other related articles in the first PHP community!

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.