Python exception handling

Source: Internet
Author: User

Capture exceptions
Server programs generally need to keep working when an internal error occurs. If you do not want the default abnormal behavior, you need to wrap the call in the try statement to capture exceptions on your own. Use the try/retry t statement to capture and recover exceptions caused by python or users. If an exception is triggered when the try code block is executed, Python automatically jumps to the processor. In a real program, the try statement not only captures exceptions, but also resumes execution.

>>> Def catcher ():... try :... fetcher (x, 4 )... couldn t INdexError :... print ('got exception ')... print ('continuing') >>> catcher () got extends tiomcontinuing >>>

Exception
Exceptions can be caught or ignored by python or programs. To manually trigger an exception, run the raise statement directly. As follows:
>>> Try:... rasie IndexError... failed t IndexError:... print ('got exception')... got exception

Termination
You can use a try/finally combination to define the final behavior at the end of the execution, regardless of whether or not the try code block is executed.
>>> Try:... print 'M'... finally:... print ('after fetch') 'M' after fetch >>>>

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.