Definition:
A custom exception is an error made by the programmer.
It is used when a user does not follow the program’s rules.
If the user enters wrong input, the programmer can show a message using a custom error.
Example:
If a program only allows the word “INDIA” and the user types something else, then the program can show: “Only INDIA is allowed.”
Now you will clearly understand the code logic using the flowchart.
Custom Exception Flowchart:
First step: → You will write the program.
↓
Second step: → You will enter the input (word = INDIA or not)
↓
Third step: → Now check the condition (word == "INDIA" ?)
If the condition is False (No), then
↓
Raise Custom Error (InvalidWord)
↓
Program stops
If the condition is True (Yes), then
↓
Execute Code
↓
Print Correct Word
↓
End the program
Syntax:
class CustomError(Exception):
pass
raise CustomError("message")
Example:
word = input("Enter word (INDIA): ")
class InvalidWord(Exception):
pass
if word != "INDIA":
raise InvalidWord("Only INDIA is allowed")
print("Correct word:", word)
Output:
