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

Exception Stack tracing and explanation of exception Utilization in python

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "exception stack tracking and exception utilization explanation in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "exception stack tracking and exception utilization explanation in python"!

I. Tracking of exception stacks

Stack characteristics: first in, then out

Exception throwing, if the inner exception is not handled, then the exception will be slowly passed from the innermost one to the outer one.

If the inner exception has been caught and handled, the inner exception is not passed.

II. Abnormal utilization

When we use the dictionary, there may often be cases where the keywords in the dictionary do not exist, so we will report the error directly. We used to use if statements to determine whether each keyword existed before it was invoked.

Note: Python case is sensitive, pay attention to the call time, case correctness.

1. Determine if key exists

The original way to judge dictionary key values prevents program exceptions

#Call dictionary output, directly use if statement to judge

if "aa" in dic1:

print(dic1["aa"])

else:

print("keyword does not exist")

Use exception catching to simplify code.

try:

print(dic1["aa"])

print(dic1["BB"])

except KeyError:

print("keyword does not exist, throw exception")

2. Determine if a class method exists

Exception utilization of classes, judgment of whether class attributes exist under the old pattern

mc=MyClass()

if(hasattr(MyClass,"abc")):

mc.abc()

else:

print("attribute does not exist")

New type of class attribute call judgment

using exception capture scenarios.

try:

mc.abc()

mc.defd()

except:

print("Property method does not exist")

III. Summary and emphasis

1. Master exception stack tracing.

2. Master the use of exceptions to optimize code and eliminate unknown exceptions.

3. Master the basic form of try catching exceptions.

4. All exceptions thrown after exception capture are exception objects.

At this point, I believe that everyone has a deeper understanding of "exception stack tracking and exception utilization explanation in python". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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