Python core programming learning diary error handling

Source: Internet
Author: User
Chapter 1 error handling
  1. Exception parameter: whether the reason contains only one string or a tuples consisting of error numbers and strings, calling str (reason) always returns a readable error cause. this is actually a special method of calling the class _ str __().
  2. Else clause: when no exception is detected in the try range, run the else clause.
  3. A finally clause is a piece of code that is executed whether or not an exception occurs. Finally can be used only with try, or with try-else t (else is also optional)
  4. The try-finally statement is not used to capture exceptions. The finally code segment is executed no matter whether exceptions occur or not.
  5. Context Management
    1. With statement: basic syntax

      with context_expr [as var]:   with_suite

      The purpose of the with statement is to remove all the Code related to the try, exist T, and finally keywords and resource allocation and release from the flowchart. With can only work on objects that support the context management protocol.

    2. Context management protocol:

      Context expression (context_expr) and context MANAGER: When the with statement is executed, the context expression is executed to obtain a context manager. It calls the _ context _ () method to provide a context object.

      Context object, with statement block: the context object calls its own _ enter _ () method to complete all preparations before the with statement block is executed. The Return Value of the method is assigned to the variable after the as. with statement block ends and the _ exit () _ method is called.

  6. Trigger exception

    Before 1.5, standard exceptions were implemented based on strings. Later, all standard exceptions were classes. Strings can be used now, but it is not recommended. (@ Deprecated)

    1. Raise statement:

      raise[SomeException [, args, [,traceback]]]

      SomeException is the name of the trigger exception and must be a string, class, or instance. Args contains the optional parameters for this exception. An exception parameter is always returned as a tuples.

      Raise without any parameters re-triggers the previous exception. If no exception exists, the TypeError is triggered.

    2. AssertionError. Syntax: assert expression [, argument]
    3. Standard exceptions are built in. They are all derived from the root exception BaseException. Directly derived Exception, SystemExit, KeyboardInterrupt. Other built-in exceptions are subclasses of Exception.
  7. Creation exception
    1. The tempfile module contains methods for creating temporary file names and temporary files.
    2. Exception and sys module

      You can use the exc_info () function in the sys module to obtain exceptions. This function returns three tuples: exc_type, exception class, exc_value, exception class instance, exc_traceback, trace (traceback) object

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.