[Turn]python inside Try statement

Source: Internet
Author: User

Exceptions for Python2010-11-03 22:36:36Python's try statement has two flavors
One: the kind is handling the exception (Try/except/else)
Two: Type is the final code will be executed regardless of whether an exception occurs (try/finally)

Try/except/else style
Try
< statements > #运行别的代码
Except < name:
< statement > #如果在try部份引发了 ' name ' exception
Except < name >,< data;:
< statement > #如果引发了 ' name ' exception to get additional data
Else
< statements > #如果没有异常发生
Try works by starting a try statement, and Python is tagged in the context of the current program so that when an exception occurs, it can go back here, the TRY clause executes first, and what happens next depends on whether an exception occurs at execution time.
1. If an exception occurs when the statement after the try is executed, Python jumps back to the try and executes the first except clause that matches the exception, and the control flow passes through the entire try statement (unless a new exception is thrown when the exception is handled).
2, if an exception occurs in the statement after the try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (This will end the program, and print the default error message).
3. If no exception occurs when the TRY clause executes, Python executes the statement after the Else statement (if there is else), and then the control flow passes through the entire try statement.


Try/finally style
Try
< statements >
Finally
< statements > #退出try时总会执行

Python always executes a finally clause, regardless of whether an exception is thrown when the try clause executes.
1. If no exception occurs, Python runs the try clause, then the finally clause, and then continues.
2. If an exception occurs in the TRY clause, Python returns to execute the FINALLY clause and then submits the exception to the upper try, and the control flow does not pass through the entire try statement.
Try/finally is useful when you want to ensure that some code is executed, whether or not an exception occurs.

This is useful when opening a file. Finally always in the last Close () file

A form of sentence form in try language
Except: Catch all exceptions
Except name: Catch only specific exceptions
Except Name,value: Catch exception and its additional data (save the exception's information to value)
Except (name1,name2): captures any listed exceptions
else: If there is no exception
Finally: Always perform

>>> Try:
f = open (' file.txt ')
Except IOError, E:
Print E
Else
print ' wrong '


[Errno 2] No such file or directory: ' File.txt '

The latest Python version supports try/except/finally

Try:1: If X does not have an exception, execute Z,i
X 2: If x has an exception, one: Execute y,i If except catches an exception
Except (name): Two: Not captured, execute I, and then return built-in exception handling
Y
Else
Z
Finally
I

[Turn]python inside Try statement

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.