In order to raise an error with a message in Python programming you can make use of the Exception class.
Let's say you want to raise an error when the provided age is less than 18, and the message you want to print is "Error! Age should be greater than 18!"
age = 15
if age < 18:
raise Exception("Error!, age should be greater than 18!")
Output:
Traceback (most recent call last):
File "./example.py", line 4, in <module>
Exception: Error!, age should be greater than 18!
As you can see in the above example: raise is the keyword that you should use to raise an exception.
Let's see another example: If the provided variable type is not str raise an error message.
name = 2k
if not type(name) is str:
raise TypeError("Error! Name should be a String value!")

Have Questions? Post them here!
More Posts related to Python,
- Read JSON File in Python Program
- Convert Float to String in Python
- Python - Convert float to String
- Check if String Contains a Substring - Python
- Install and Run Jupyter Notebook on Mac (macOS)
- Sorting an array using Bubble Sort in Python Programming
- Comments in Python Programming
- 3 Python program to add two numbers
- Read a file line by line in Python Program
- How to uninstall pip Python packages
- Change label (text) color in tkinter
- 7 Python Arithmetic Operators with Examples [Tutorial]
- pip get list of all outdated Python packages
- Where does brew install python in macOS
- How to Convert String to DateTime in Python
- Python: Fix command not found pip or pip3 on zsh shell
- How to add borders to tkinter label text
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- TypeError: must be str, not int
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Check version of pip package installer for Python
- Change the background of Tkinter label or text
- Set width and height for the label in tkinter
- How to write JSON file in Python Program
- Python raise error with message example
More Posts:
- XmlRpcException ConnectException connection refused error - Java
- Free and easy audio video screen recording using Microsoft PowerPoint - Microsoft
- Android appcompat_v7 Error retrieving parent for item: No resource found that matches the given name - Android
- Todays Apple Spring Loaded Event Live Updates - Apple
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP - JavaScript
- [Fix] Java Exception with Lambda - Cannot invoke because object is null - Java
- How to Change Text Size for Android ActionBar - Android
- Android Toolbar example with appcompat_v7 21 - Android