Python errors and exceptions

Source: Internet
Author: User

1. Try {} catch {} in Python {}

2. Raise exception

3. try... try t... else ..

4. Finally Blocks

1. Try {} catch {} in Python {}

The keyword for exception handling in python is different from that in C #. The key for using try and retry t in python is as follows:

DefDive (x, y ):
Try :
Result
= X / Y;
Except Zerodivisionerror as Z:
Print ( " Division by zero. " , Z );
Else :
Print ( " The result is " , Result );
Finally :
Print ( " Executing finally caluse. " );

 

2. Raise resume ption

In python, if you need to throw an exception in counter T, you can use the keyword raise, similar to the throw keyword in C.

Def Raise_test ():
Try :
Result
=   1   / 0;
Except Zerodivisionerror:
Print ( " Can not process the exception, to throw... " );
Raise ;
Finally :
Print ( " In finally Block " );
Raise_test (); 


3. try... try t... else ..

 

It is useful for code that must be executed if the try clause does not raise an exception

The else block is executed if the exception is not thrown.

4. Finally Blocks

 

A finally clauseis always executed before leaving thetrystatement

Finally blocks are executed, regardless of whether exceptions are generated.

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.