Exception capture method in python

Source: Internet
Author: User
Exception information acquisition is very important for program debugging and can help you quickly locate the wrong program statements. The following describes several methods to obtain exception information in python. we hope that you will like to use the try-try T code block to handle exceptions in Python, and put the try-try T code block into the operations executed by python, at the same time, tell the python program what to do if an exception occurs. In fact, the try-try T function has been put into the advanced space in many entry books. This function is generally not used at the entry level, especially as an O & M personnel, if you often write shell, it is estimated that this function is rarely used after conversion to python. this function I think shows an important difference between shell and python, because python is a real programming language, and other programming languages such as php and java provide exception capture functions, the code written using these programs must be robust, if you read some program code written by others, especially some socket programming code, many of them are try... Else t ..., Some of them are too many to judge various situations. Since this function is so useful, let's get to know how to use it as soon as possible.

First, let's look at its syntax. The syntax is very simple, that is, put the code block you want to execute in try-example T, for example :,

Try: somecode1except exception type/Name: somecode2

This is the simplest case. if the situation is complex, you can use multiple sentence T, for example:

Try: somecode0except exception type/name 1: somecode1t T exception 2: somecode2except exception 3: somecode3

Try-try T has more advanced usage, such as else and finally. today we will not talk about it. if you are interested, you can study it in depth.

Next let's take a simple example. we often perform operations on reading and writing files. a common problem is that the file cannot be found, or the file name or path is incorrect. in this case, you can use the try-try T code block to handle the problem intuitively:

try:  withopen(filename, 'r+') as fp:    data = fp.read()exceptIOError:  msg = 'sorry, can not read or write this ' + filename  printmsg

Let's look at an example of multi-digit T, where two digits are subtracted:

Loop = 1 while loop = 1: try: a = input ('Enter the first number> ') B = input ('Enter the second number>') then tnameerror: print "enter a number. do not enter the letter" continue into tsyntaxerror: print ". enter only one number. "continue print a-B try: loop = input ('Start after 1> ') using T (NameError, SyntaxError): loop = 0

The above two examples are the simplest try-train t usage. if you want to ensure the robustness of the script, you can use the try-train T code block later, this will make your code look more professional.

For more articles about exception capture methods in python, refer to PHP Chinese network!

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.