Python runtime exception management solution

This article describes the Python runtime exception management solution, the paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer
1 Introduction

Python uses a special object called an exception to management errors that occur during program execution. Whenever an error occurs so that a loss of Python, it creates an exception object.

If you write code to handle the exception, the program will continue to run; if you do not to deal with exceptions, the program will stop and display a traceback, which contains reports about the exceptions.

Exceptions are the use of try-except block processing. try-except block let Python perform the specified action, and tell how to do Python exception occurs.

When using a try-except block, even if abnormal, the program will continue to run.

2 using try-except block

We know that the divisor is not 0, and when a programmer mistakenly divisor is set to 0, ZeroDivisionError error will occur.

Implementation code:

try:
  print(6/0)
except ZeroDivisionError:
  print("you can not divide by zero!")

operation result:

you can not divide by zero!

working principle:

The possible error code in try block, if error-free operation, except the code block will be skipped; if the error cause, Python code blocks except the look and executes the code.

As a result, when run error, the user sees is a friendly error message.

If the try-except behind the other codes, the program will then run.

3 using try-except-else block

Implementation code:

print("Please input two numbers, and I will divide them.")
print("Enter 'q' to quit.")
 
while True:
  first_num = input("\nFirst number: ")
  if first_num == 'q':
    break
  second_num = input("Second number: ")
 
  try:
    answer = int(first_num) / int(second_num)
  except ZeroDivisionError:
    print("you can not divide by zero!")
  else:
    print(answer)

The result: Here Insert Picture Description
it works:

Generally, the exception may occur in a try code block.

Attempt to execute Python try block code, if an error occurs, the except the program code is executed; if the normal operation, the code else code block is executed.

As a result, it effectively avoids bugs caused by crashes that may occur, so that the program becomes more robust.

4 using pass sentence

Implementation code:

while True:
  first_num = input("\nFirst number: ")
  if first_num == 'q':
    break
  second_num = input("Second number: ")
 
  try:
    answer = int(first_num) / int(second_num)
  except ZeroDivisionError:
    pass
  else:
    print(answer)

The result:
Here Insert Picture Description
it works:

10 When the error code is executed, Python pass statement will be executed except the code block. The pass statement neither appear traceback, there is no output.

Further, pass statement may also act as a placeholder for the programmer to alert any statement where not defined, may later definition statement.

5 Summary

Here, we know some way to handle exceptions, we can according to different needs, decide whether to report the error to the user and how to report errors, effectively avoid abnormal program caused the collapse.

I write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share

Experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information,

Python day to you on the latest technology, prospects, learning to leave a message of small details

Published 48 original articles · won praise 21 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/105082003