Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to handle multiple exceptions in python

Shulou Source: shulou.com Published: 2022-06-02 03:17:23 09月22日 Update

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.

Tags: Processing multiple case content alias detail actual that is method case knowledge class name result statement classification output usage information key keyword Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Shulou Information Redmi Apple