In Python, exception handling is used to determine the operating system platform method. In python, Exception Handling

Source: Internet
Author: User

In Python, exception handling is used to determine the operating system platform method. In python, Exception Handling

Code example:
Copy codeThe Code is as follows:
Try:
Import termios, TERMIOS 1
Failed t ImportError:
Try:
Import msvcrt 2
Failed t ImportError:
Try:
From EasyDialogs import AskPassword 3
Failed t ImportError:
Getpass = "default_getpass" 4
Else:
Getpass = "AskPassword" 5
Else:
Getpass = "win_getpass"
Else:
Getpass = "unix_getpass"

1: termios is a unique module of UNIX. It provides underlying control over the input terminal. If this module is invalid (because it is not on your system, or your system does not support it), the import fails, and Python triggers the ImportError exception.

2: OK. We don't have termios, so let's try msvcrt. It is a unique module in Windows, an API that provides many useful functions in Microsoft Visual C ++ running services. If the Import fails, Python will cause an ImportError exception.

3: If the first two cannot work, we try to import a function from EasyDialogs, which is a unique module of Mac OS and provides various types of pop-up dialog boxes. Once again, if the Import fails, Python will cause an ImportError exception that we captured.

4: None of these platform-specific modules are valid (probably because Python has been transplanted to many different platforms ), so we need to use a default password input function (this function is defined elsewhere in the getpass module ). Note what we have done here: we assign the default_getpass function to the variable getpass. If you read the official getpass document, it will tell you that the getpass module defines a getpass function. It does this: bind getpass to the correct function to adapt to your platform. Then, when you call the getpass function, you actually call the platform-specific function. This Code has already been set for you. You do not need to know or care about the platform on which your code is running. If you call getpass, it can always be handled correctly.

5: a try... limit t block can have an else clause, just like an if statement. If no exception is thrown in the try block, the else clause is executed. In this example, it means that if the import from EasyDialogs import AskPassword can work, we should bind getpass to the AskPassword function. Each Other try... example t block has a similar else clause. When we find that an import is available, we bind getpass to the appropriate function.

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.