[PYTHON] Create your own exception

python


class UppercaseError(Exception):
    pass

def check():
    words = ['APPLE', 'orange', 'banana']
    for word in words:
        if word.isupper():
            raise UppercaseError

try:
    check()
except UppercaseError as e:
    print('This is an error I created.')

Execution result


This is an error I created.

Exception inherited as a superclass Create a subclass UppercaseError.

The content of UppercaseError is pass, that is Same as Exception.

Create a check function. The contents of the check function are the contents of the words list one by one. Check for anything in capital letters and If there is a raise UppercaseError In other words, it is called UppercaseError.

Below try Execute the check function, If UppercaseError occurs except UppercaseError The block is executed.

So In the execution result, This is an error I created. Is output.

Recommended Posts

Create your own exception
Create your own Django middleware
How to create your own Transform
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Create your own DNS server with Twisted
Create your own Composite Value with SQLAlchemy
Create a wheel of your own OpenCV module
Memo to create your own Box with Pepper's Python
Create your own Big Data in Python for validation
Create your own Random Dot Stereogram (RDS) in Python.
[Blender × Python] Create your own function & summary so far
Reinforcement learning 23 Create and use your own module with Colaboratory
[Python] Make your own LINE bot
Make your own manual. [Linux] [man]
[Python] logging in your own module
Solve your own maze with Q-learning
Try implementing k-NN on your own
Create your own graph structure class and its drawing in python
Try docker: Create your own container image for your Python web app
Until you self-host your own interpreter
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Bridge ROS to your own protocol
Train UGATIT with your own dataset
Solve your own maze with DQN
Let's think about judgment of whether it is PDF and exception handling. Until you create your own exception handling
Create your own virtual camera with Python + OpenCV and apply original effects
[Introduction to pytorch-lightning] How to use torchvision.transforms and how to freely create your own dataset ♬
Add your own content view to mitmproxy
Your own Twitter client made with Django
[Reinforcement learning] DQN with your own library
Create wordcloud from your tweet with python3
Easily use your own functions in Python
Migrate your own CMS data to WordPress
Create a model for your Django schedule
Create a python environment on your Mac
[Python] Package and distribute your own modules
Note on building your own Miniconda environment
Make your own PC for deep learning
Tweet your own sentences using Markov chains
[Python] Register your own library on PyPI
Until you install your own Python library
To import your own module with jupyter
Import your own functions on AWS Glue
How to install your own (root) CA
Create your first app with Django startproject
Publish your own Python library with Homebrew
Get your own IP address in Python
[For recording] Keras image system Part 1: How to create your own data set?
[Machine learning] Create a machine learning model by performing transfer learning with your own data set