Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the python exception output

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to optimize the python exception output". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to optimize the python exception output.

Abnormal information output by one line of code optimization

Pip install pretty-errors

Write a function test:

Def divided_zero ():

For i in range (10-1,-1):

Print (10Universe)

Divided_zero ()

Before the pretty-errors of import, the error message output was somewhat redundant:

Traceback (most recent call last):

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ ptvsd_launcher.py", line 43, in

Main (ptvsdArgs)

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ lib\ python\ old_ptvsd\ ptvsd\ _ _ main__.py"

Line 432, in main

Run ()

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ lib\ python\ old_ptvsd\ ptvsd\ _ _ main__.py"

Line 316, in run_file

Runpy.run_path (target, run_name='__main__')

File "D:\ anaconda3\ lib\ runpy.py", line 263, in run_path

Pkg_name=pkg_name, script_name=fname)

File "D:\ anaconda3\ lib\ runpy.py", line 96, in _ run_module_code

Mod_name, mod_spec, pkg_name, script_name)

File "D:\ anaconda3\ lib\ runpy.py", line 85, in _ run_code

Exec (code, run_globals)

File "d:\ source\ sorting-visualizer-master\ sorting\ debug_test.py", line 6, in

Divided_zero ()

File "d:\ source\ sorting-visualizer-master\ sorting\ debug_test.py", line 3, in divided_zero

Print (10Universe)

ZeroDivisionError: division by zero

We use the pretty_errors,import we just installed:

Import pretty_errors

Def divided_zero ():

For i in range (10-1,-1):

Print (10Universe)

Divided_zero ()

At this point, take a look at the output error message, very concise only 2 lines, to those redundant information:

ZeroDivisionError:

Division by zero

The complete output is shown in the picture below, with a split line in the middle, and then output the stack of the exception, followed by a simplified exception name and prompt.

At this point, I believe that everyone on the "python exception output how to optimize" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report