Python try/except/finally, etc.

Source: Internet
Author: User

[code block]
    1. x = ' abc '
    2. def fetcher (obj, index):
    3. return Obj[index]
    4. Fetcher (x, 4)

Output:

    1. File "test.py", line 6, <module>
    2. Fetcher (x, 4)
    3. File "test.py", line 4, in Fetcher
    4. return Obj[index]
    5. Indexerror:string index out of range


First: Try not only catches exceptions, but also resumes execution

    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 4)
    4. except:
    5. print "Got exception"
    6. print "continuing"

Output:

    1. Got exception
    2. Continuing


Second: The finally will execute regardless of whether or not the try has an exception

    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 4)
    4. finally:
    5. print ' after Fecth '

Output:

    1. After FECTH
    2. Traceback (most recent):
    3. File "test.py", line-in <module>
    4. Catcher ()
    5. File "test.py", line catcher
    6. Fetcher (x, 4)
    7. File "test.py", line 4, in Fetcher
    8. return Obj[index]
    9. Indexerror:string index out of range


Third: Try no exception to execute else

    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 4)
    4. except:
    5. print "Got exception"
    6. Else:
    7. Print "not exception"

Output:

    1. Got exception
    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 2)
    4. except:
    5. print "Got exception"
    6. Else:
    7. Print "not exception"

Output:

    1. Not exception

Else: There is no else statement, and when the try statement is executed, there is no way to know whether an exception has occurred or an exception has occurred and has been processed. Can be clearly separated by else.

IV: Using raise to transfer anomalies

    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 4)
    4. except:
    5. print "Got exception"
    6. Raise

Output:

    1. Got exception
    2. Traceback (most recent):
    3. File "test.py", line Notoginseng, in <module>
    4. Catcher ()
    5. File "test.py", line A, in catcher
    6. Fetcher (x, 4)
    7. File "test.py", line 4, in Fetcher
    8. return Obj[index]
    9. Indexerror:string index out of range

The current exception is re-thrown when the raise statement does not include the exception name or additional information. If you want the capture to handle an exception, and you do not want

The exception disappears in the program code and can be re-thrown by raise.

V: Except (name1, name2)

    1. Def catcher ():
    2. Try:
    3. Fetcher (x, 4)
    4. except (TypeError, indexerror):
    5. print "Got exception"
    6. Else:
    7. Print "not exception"

Captures the list of exceptions that are listed for processing. If there are no arguments after except, all exceptions are caught.

      1. Def catcher ():
      2. Try:
      3. Fetcher (x, 4)
      4. except:

Python try/except/finally, etc.

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.