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