10. Exception Handling, custom exceptions, assertions, and Exception Handling assertions

Source: Internet
Author: User

10. Exception Handling, custom exceptions, assertions, and Exception Handling assertions
What is an exception:

  • When a program encounters some abnormal problems, it throws an exception: for example, int () can only process objects that can be converted to int. If an object that cannot be converted is input, an error is reported and an exception is thrown.
  • Common exceptions include:

ValueError: input an invalid parameter.

TypeError: the type is invalid.

IndexError: This index is not found in the sequence.

NameError: Use undefined Variables

For more specific exceptions, refer to the Python official documentation to read the official documentation for better health.

Exception Handling:
  • Python uses try, finally T, finally, and else to handle exceptions. Their respective meanings are as follows:

Try: s = input ("Enter the string to be int:") I = int (s) Counter t ValueError as e: print (e) else: print ("here is the operation when no exception occurs") finally: print ("here is the code that will be executed even if an exception occurs") "the first running result: enter the string to be int: 123 here is the operation when no exception occurs. Here is the code "that will be executed even if an exception occurs. For the second running result, enter the string to be int: abcinvalid literal for int () with base 10: 'abc' here is the code that will be executed even if an exception occurs """
  • Except t can capture multiple exceptions in one commit t, but multiple exceptions need to be wrapped in parentheses. Even if multiple exceptions are caught at the same time, the program will be terminated due to exceptions, therefore, only one exception is caught. Therefore, only one variable is required to get the returned object due to the exception:
  • If you are not sure what exceptions are caught, you can use the Exception base class Exception [more advanced, you can use BaseException], and it is best to put Exception at the end of all except T:

 

Custom exception:
  • Raise can be used to throw custom exception information.
  • As long as the Exception is inherited, you can define your own Exception class:
Class myException (Exception): def _ init _ (self, msg): self. message = msg def _ str _ (self): ### here is the abnormal string information return self. messagetry: raise myException ("My exceptions") # py3failed t myException as e: print (e) "result: My exceptions """

Assertions:
  • Meaning: instead of making the program run the most collapsed, it is better to crash when an incorrect condition occurs. For example, it is highly risky for an old driver to drive a car at will, using assertions is like first determining the road conditions and then driving, rather than driving out to find the traffic police brigade waiting]
Police = Falseassert police = Falseprint ("old driver is driving, whining ")

 

Police = Trueassert police = Falseprint ("old driver is driving, whining") # error, AssertionError

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.