Python learns the third bullet: How is the exception handled?

Source: Internet
Author: User

Python's Way of handling errors:
1> Assertion
    assert condition    equivalent      to if not condition:        The crash    program asserts that the purpose of the setting is to cause it to crash later , instead of directly setting the error condition, let it    crash directly >>> age =-1    assert 0 < Age <    Assertionerror 

2> Catching exceptions
    Try: x=input () y=input ()Print(x/y)exceptZerodivisionerror:Print("Division by Zero") Like this try/The except form is to catch possible exception errors, and once the Zerodivisionerror error message occurs, the code after the except Zerodivisionerror is run through multiple except to catch different error messages defCalc (expr):Try:                returneval (expr)exceptZerodivisionerror:Print("Divison by Zero")            exceptTypeError:Print('This is not a number?') a block captures multiple error messagesdefCalc (expr):Try:                returneval (expr)except(Zerodivisionerror, TypeError):Print("Input has some bugs") Print error messagedefCalc (expr):Try:                returneval (expr)except(Zerodivisionerror, TypeError) as Error:Print(Error) Information processing for other errorsdefCalc (expr):Try:                returneval (expr)exceptZerodivisionerror:Print("Divison by Zero")            except:                Print('haha, something unknown happened!'Else statement is used to avoid error messagesdefCalc (expr):Try:                returneval (expr)exceptZerodivisionerror:Print("Divison by Zero")            Else:                Print("Oh, it goes well!"There is also a finally statement that performs the actual application, whether or not the error is true: (input expression until it can be evaluated) whileTrue:Try:            Print(eval (input ()))except:            Print('reinput until you input the correct expression!')        Else:             Break; finally:            Print('I love the World whatever!')#even if the else break also executes finally

Python learns the third bullet: How is the exception handled?

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.