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 handle multiple exceptions in python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to deal with multiple exceptions in python, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

First, handle multiple exceptions

1. Refine all the exceptions, determine the exception conditions, deal with the known exceptions directly, and deal with them respectively.

In this case, the keyword except is used many times to handle exceptions.

two。 Handle all exceptions uniformly, classifying multiple known exceptions together.

We categorize several specific exceptions together and handle them in the same way. We write multiple exceptions to the same except in parentheses, with the exceptions separated by commas.

Except (MyCustomerException1,MyCustomerException2):

Print ("the results of exception 1 and exception 2 are handled here")

3. Handle other unknown exceptions.

The trick for handling unknown exceptions is not to add an exception after except, where all unknown exceptions are handled by default.

Second, the case: do multiple exception handling cases

1. Customize multiple exceptions

two。 Call several custom exceptions according to the actual situation

3. Handling exceptions

Third, catch exceptions and take aliases

In try... The actual exception after the except statement in the except statement. If the class name is too long, we can take an alias. Format except class common class name as alias

Except MyCustomerException1 as mce1:

IV. Summary and emphasis

1. The method of knowing all the abnormal situations is try. Except

two。 Master the handling methods of custom exceptions

3. Master the detailed handling of exceptions

4. Master the information input and output of the constructor of a custom exception

5. Master using the same except to handle multiple exceptions

This section knowledge source code

# the first custom exception class MyCustomerException1 (Exception): def _ _ init__ (self,code=100,desc= "exception message: 100"): self.code=code self.desc=desc

# Custom the second exception class MyCustomerException2 (Exception): pass

A=input ("Please output a value:")

# try:# if exception = "qqq": # raise MyCustomerException1# elif exception = "123": # raise MyCustomerException2 ("exception here is 2") # else:# b=a/0# except MyCustomerException1 as mce1:# print ("first exception triggered here", mce1.code,mce1.desc) # except MyCustomerException2:# print ("second exception triggered here") # except:# print ("unknown exception is handled here")

Try: if exceptions = "qqq": raise MyCustomerException1 elif exceptions = "123": raise MyCustomerException2 ("here the exception is 2") else: b=a/0except (MyCustomerException1,MyCustomerException2): print ("the results of exception 1 and exception 2 are handled here") except: print ("unknown exception")

After reading the above, do you have any further understanding of how to handle multiple exceptions in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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