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 use custom exception classes in python

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

Share

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

This article shows you how to use custom exception classes in python, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Custom exception class

1. Custom CLA

two。 Inherit the exception base class exception of the system

3. Customize the constructor of the exception class and other methods to handle

For example:

Customize my exception class MyException

This is the simplest exception class.

Class MyException (Exception):

Pass

Case: judging the status of input

If it is not a numeric value, throw an exception.

A=input ("please enter a value?")

If not a.isdigit ():

Raise MyException ("exception: you did not enter a numeric value!")

The summary emphasizes:

1. Custom exception class.

two。 Learn how to take advantage of custom exception classes.

3. The construction method of custom exception class is used to prompt the exception value.

The knowledge source code of this section:

# Custom exception class class MyException (Exception): def _ _ init__ (self,a): self.a=a

# use a=input for the simplest exception class ("Please enter a value?")

If not a.isdigit (): raise MyException (a) the above is how to use custom exception classes in python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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