Python exception: Try, expect, finally

Source: Internet
Author: User
Tags integer division

A little summary, otherwise always forget.

[python] view plaincopyprint?

1. x = ' abc '

2. def fetcher (obj, index):

3. return Obj[index]

4.

5. Fetcher (x, 4)

Output:

[Plain] view plaincopyprint?

1. File "test.py", line 6, in <module>

2. Fetcher (x, 4)

3. File "test.py", line 4, in Fetcher

4. Return Obj[index]

5. Indexerror:string index out of range

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

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. except:

5. Print "Got exception"


The first: Try not only catches exceptions, but also resumes execution

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. except:

5. Print "Got exception"

6. Print "continuing"

Output:

[Plain] view plaincopyprint?

1. Got exception

2. Continuing


Second: No matter whether a try or not an exception, finally always execute

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. finally:

5. print ' after Fecth '

Output:

[Plain] view plaincopyprint?

1. After FECTH

2. Traceback (most recent call last):

3. File "test.py", line <module>

4. Catcher ()

5. File "test.py", line, 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


Third: Try no exception to execute else

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. except:

5. Print "Got exception"

6. Else:

7. Print "Not exception"

Output:

[Plain] view plaincopyprint?

1. Got exception

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 2)

4. except:

5. Print "Got exception"

6. Else:

7. Print "Not exception"

Output:

[Plain] view plaincopyprint?

1. Not exception

Else action: There is no else statement, and when the try statement is executed, it is not known whether an exception has occurred, or an exception has occurred and has been processed. Separated by what else can be clear.

Fourth: Using raise to pass exceptions

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. except:

5. Print "Got exception"

6. Raise

Output:

[Plain] view plaincopyprint?

1. Got exception

2. Traceback (most recent call last):

3. File "test.py", line Panax Notoginseng, in <module>

4. Catcher ()

5. File "test.py", line, 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

When the raise statement does not include an exception name or additional data, the current exception is raised again. If you want to catch a handle to an exception, and you don't want to

The exception disappears in the program code and can be raise again by using the.

Fifth: Except (name1, name2)

[python] view plaincopyprint?

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 exceptions listed in the list for processing. If there are no arguments after except, all exceptions are caught.

[python] view plaincopyprint?

1. def catcher ():

2. Try:

3. Fetcher (x, 4)

4. except:

5. Print "Got exception"

Sixth: Exceptions and SYS modules

Another way to get exception information is through the Exc_info () function in the SYS module. The function returns a ternary group: (Exception class, instance of exception class, following Record object)

[python] view plaincopy

1. >>> try:

2... 1/0

3... except:

4... Import sys

5. Tuple = Sys.exc_info ()

6...

7. >>> Print tuple

8. (<type ' exceptions. Zerodivisionerror ' ", Zerodivisionerror (' integer division or modulo by zero"), <traceback object at 0X7F538A318B48 >)

9. >>> for i in tuple:

Print I

11...

12.<type ' exceptions. Zerodivisionerror ' > #异常类

13.integer Division or modulo by zero #异常类的实例

14.<traceback object at 0x7f538a318b48> #跟踪记录对象


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.